19th Aug 2007
Removing file extension via .htaccess
Problem:
You have the following URLs for your website:
www.example.com/about-us.html
www.example.com/services.html
www.example.com/contact-us.html
However, you would like to hide file extensions from the end users, and allow them to access to the files using the following URLs:
www.example.com/about-us
www.example.com/services
www.example.com/contact-us
Solution:
The solution can be achieved by using Apache’s mod_rewrite. Create an .htaccess file in your website root directory with the following content.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.html$ $1 [L]
# Replace html with your file extension, eg: php, htm, asp
Benefits:
- Search engine friendly
- Easier to read and remember
- Extension/environment independent; when you change the technology used for your website (for eg: from using asp to php), you can be assured that all the links and bookmarks will still work.
Technorati Tags: htaccess, mod rewrite, url rewriting, nice urls, seo
Referral text link:
ERRRH!!! Your code doesn’t work.
I got this error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, support@bonaway.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
The real working code should be like this:
For PHP
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php