Using mod_rewrite to remove variables from a URL

The Apache mod_rewrite module offers an unparalleled toolset for manipulating URLs, – however configuration can be a tricky beast at times. While most people are comfortable enough removing file extensions like .php we recently had a user who wanted to hide all the variables in the address string.

For example index.php?id=id would end up being index/id
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about/(.*)$ index.php?id=$1 [QSA]

The magic here is the QSA or Query String Append. If you just wanted to remove the .php you would use
RewriteCond %{REQUEST_URI} !.*\.php$
RewriteRule ^/(.*)$ $1.php [QSA]

Leave a Reply

Your email address will not be published. Required fields are marked *