Hide file extensions in URLs with .htaccess files

I was working on a (rather neglected, outdated) site I had previously done and decided it needed some (rather, a lot of) improvements. I was looking for a way to get rid of to clean up the URLs without having to remap my files. .htaccess to the rescue!

.htaccess files are a wonderful thing, and incredibly powerful, ignoring the bad experience I had with them on my current web host (fortunately, this project is hosted by another web host, and everything runs perfectly with them :)), and Apache’s mod_rewrite module does exactly what we need.

There are a bunch of reasons you’d want to do this, mainly:

it looks cleaner and easier to read and remember
by using search-engine–friendly URLs, you’re Google PageRank will increase and will increase your website’s findability
it makes your website environment-independent, so if you ever decide to change the technology your site uses, everything would appear seamless to your visitors.
In this case, we’re using PHP files, but you can change it to whatever type of file you’re using, be it .html, .asp, .cfm, or anything else, as long as they’re all the same type. (If you want to do this for multiple file types, just copy lines 2–4 and apply the same technique accordingly.)

Open your text editor and create a file called “.htaccess” with the following code in it, and upload it to your site’s root directory (Note: On Unix and unix-like operating systems, files that start with a dot are hidden files, so you may not be able to see the file after you save it. To get around this, omit the preceding dot when naming the file, and then rename the file back to “.htaccess” after you have uploaded it to your webserver):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
(adapted from the Apache mailing list)

Using this code, instead of having to type in http://mysite.com/contact.php, you only need to enter http://mysite.com/contact to access that page. And the best part is, you can still access the page with .php on the end of it, so no old incoming links or bookmarks become orphaned as a result of this, and everyone is happy.

what is the benefits of rewriting URL?
When a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees “?” sign treat it as a url which keeps on changing. so we’re converting the dynamic URL like the product.php?id=5 to static url format like product-5.html. We’ll rewrite the url in such a way that in browser’s address bar it will display as a product-5.html but it actually calls the file product.php?id=5. So that why these kind of URL also named as SEO friendly URL.
what is required for URL rewriting ??
To rewrite the URL you must have the mod_rewrite module must be loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know much about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?
Examples of url rewriting for seo friendly URL

For rewriting the URL, you should create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).htm$ $1.php [nc]
The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+).html$ products.php?id=$1
The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.

Leave a Reply

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


seven − 5 =