I wrote this code to parse the kind of urls you want to use into an array of parameters. It's flexible, meaning you can use the parameters in totally different ways on every page. You will need it use it with a request handler like Joel L suggested.
It also allows for old-school $_GET query strings to be understood just in case, and it also overwrites $_SERVER['PHP_SELF'] to behave like you expect it to, with the path to the controlling script rather than the entire url with all your parameters.
I quickly edited it for use as a stand alone function, so watch out for typos. Also, any suggestions to improve this code are welcome.
<?php
function parseRequestUrl($http_server = 'http://example.com'){
$request = $_SERVER['REQUEST_URI'];
$request = explode('?', $request);
if (isset($request[1])){
$get_data = $request[1];
parse_str($get_data, $_GET);
}
$request = $request[0];
$url_data = array_values(array_filter(explode('/', $request)));
$url_level = substr_count($http_server, '/', 8);
foreach($url_data as $key => $val){
if($key>$url_level){
$parameter[] = $val;
} else if ($key==$url_level){ // controller is also stored at $this->parameter[0]
$parameter[] = $controller = $val;
}
}
if(!isset($controller) || !$controller){
$parameter[0] = $controller = 'index';
}
$_SERVER['PHP_SELF'] = $http_server.'/'.$controller;
return $parameter;
}
?>
For $http_server use your site's domain.
So if you use this on the request of:
http://site.com/news/category/arts/page/5
You will get the following variable back:
$parameter[0] => 'news'
$parameter[1] => 'category'
$parameter[2] => 'arts'
$parameter[3] => 'page'
$parameter[4] => '5'