Port PATH_INFO to mod_rewrite style SEF URLS
I've never got round to actually writing about code much on this blog - till now. I've been updating a PHP directory system (because I will be launching one soon) that was written some time ago for PHP4. That system supports SEF (Search Engine Friendly URLs) in this style:www.example.com/index.php/get/varables/like/thisThis was done using this PHP feature: $_SERVER['PATH_INFO']
Now I want a better keyword saturation (hey - index.php there is NOT helping!) by using Apache's mod_rewrite feature (there is something similar for IIS by the way but its a third party module) so I get a URL that looks like this instead:
www.example.com/get/varables/like/thisIts pretty easy to do but this is the code change I made:
//$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';The old code is the commented line above.
$path = substr( $_SERVER['REQUEST_URI'], strrpos( $_SERVER['SCRIPT_NAME'],'/' ));
Now to support this the last thing that is needed is (hats off to Mambo who use this widely) is the require directives in the apache config files or the .htaccess file:
RewriteEngine OnThis code does nothing for a real file/directory but for everything elses passes the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
Cheers
No comments:
No trackbacks:
Trackback link:
Please enable javascript to generate a trackback url