Clean URLs with mod_rewrite and .htaccess
Do you stop and think that the .php file extension on your files looks ugly and unnecessary? I do. When I’m creating a project, one of the first things I do is get rid of those file extensions.
Here’s a quick and easy way to do that using <a href=”http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html”>mod_rewrite</a> in your .htaccess file. (<i>Note: You will need to have the mod_rewrite module enabled, and the directory will need <a href=”http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride”>AuthOverride</a> All on, in the apache configuration</i>)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
This code basically tells mod_rewrite that if the requested filename is not a file, and not a directory (the first 2 lines), then rewirte the URL as %{REQUEST_FILENAME}.php. Take this code, create a new file in your web root, named .htaccess, paste the code in and save it. That’s all there is to it.
So if you have a file called “file.php” on your domain (http://example.com/), you should now be able to access it by going to http://example.com/file rather than http://example.com/file.php.