mod_rewrite, php and the .htaccess file

One approach is to rewrite everything to a handling script

RewriteEngine on
RewriteBase /

# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# pass the rest of the request into index.php to handle     
RewriteRule ^(.*)$ /index.php/$1 [L]

so if you have a request to http://yourserver/foo/bar/

what you actually get is a request to http://yourserver/index.php/foo/bar – and you can leave index.php to decide what to do with /foo/bar (using $_SERVER[‘PATH_INFO’] -tom)

You only need to modify .htaccess the first time. All future requests for inexistent files can then be handled in PHP.

You might also find the docs for mod_rewrite useful – but keep it simple or prepare to lose a lot of sleep and hair tracking down obscure errors.

Leave a Comment