.htaccess :
RewriteEngine on
# skip rewriting if file/dir exists (optionally)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite all to results.php
RewriteRule . results.php
php (simple way with key=>value pairs):
// current URI (/jobs/find/keyword/accounting-finance/state/NSW/type/free-jobs/page/1/?order=1)
$path = $_SERVER['REQUEST_URI'];
// remove base path (/jobs)
if (($len = strlen(basename($_SERVER['SCRIPT_FILENAME']))))
$path = substr($len, $path);
// remove GET params (?order=1)
if (false !== ($pos = strpos($path, '?')))
$path = substr($path, 0, $pos);
$path = explode('/', trim($path, '/'));
// extract action (or whatever 'find' is)
$action = array_shift($path);
// make key => value pairs from the rest
$params = array();
for ($i = 1, $c = count($path) ; $i < $c ; $i += 2) {
$params[urldecode($path[$i - 1])] = urldecode($params[$i]);
// or put it to GET (only remember that it will overwrite already existing values)
//$_GET[urldecode($path[$i - 1])] = urldecode($params[$i]);
}
you can modify this script to achieve values only without keys, but here comes the question - is it possible to determine if value should be one key or another? If params are always on same position and you can only get less or more of them, then its pretty easy:
// skip this step from previous example
//$action = array_shift($path);
$params = array(
'action' => null,
'keyword' => null,
'state' => null,
'type' => null,
'page' => null,
);
$keys = array_keys($params);
for ($i = 0 , $c = min(count($path), count($keys) ; $i < $c ; ++$i) {
$params[$keys[$i]] = urldecode($path[$i]);
}
But if you don't know which param is on which position then things are going to be more complex. You would need to do some checks on every param and determine which one it is - if all of those values are selected from some known lists of values then it also won't be very difficult, for example:
$params = array(
'action' => null,
'keyword' => null,
'state' => null,
'type' => null,
'page' => null,
);
$params['action'] = array_shift($path);
$keys = array_keys($params);
foreach ($path as $value) {
if (is_numeric($value)) $params['page'] = intVal($value);
else {
$key = null;
// that switch is not very nice - because of hardcode
// but is much faster than using 'in_array' or something similar
// anyway it can be done in many many ways
switch ($value) {
case 'accounting-finance' :
case 'keyword2' :
case 'keyword3' :
$key = 'keyword';
break;
case 'NSW' :
case 'state2' :
$key = 'state';
break;
case 'type1' :
case 'type2' :
case 'type3' :
$key = 'type';
break;
// and so on...
}
if ($key === null) throw new Exception('Unknown value!');
$params[$key] = $value;
}
}
You can also to try write some really complex regexes in .htaccess, but IMO it is not a place for that - apache should match request with correct endpoint in your application and run it, its not place for extended params logic (if anyway it will go to the same place in your app). Also its much more convenient to keep that logic in app - when you're changing something you can do that in app code without need of changing anything in htaccess or apache config (in productional environment I'm mostly moving .htaccess contents to apache config and turning off .htaccess support - that gives some speedup when apache is not searching for those files, but any change require apache restart).