Using mod_rewrite (apache module) URL rewriting – For enforcing single URL for website home

I used apache rewrite module while making home page of our main website accessible through one link only which was required for search engine optimization. Generally you can access any website in following ways:

1. Go to http://www.abc.com
2. Go to http://abc.com
3. Go to http://abc.com/index.html
4. Go to http://www.abc.com/index.html

Essentially these all links above will land on single page which is index page of website but from search engine these are different URLs and which can create a possibility that it is treated differently. Visits to the same page can be counted as visits to different URLs of same website. Search engines generally index pages against one URL for dynamic URLs and for making this easier we can use apache module mod_rewrite.

There is another use of this module for making your website URLs little more user friendly e.g. converting http://www.abc.com?cat=5&item=89&tr=3454 to http://www.abc.com/books/fiction/newbooks , which is more understandable by user in address bar of his browser.

 
<VirtualHost www.techiegyan.com:80>
     DocumentRoot /home/techiegyan/public_html
     ServerName www.techiegyan.com
     ServerAlias techiegyan.com
     RewriteEngine On
     RewriteCond %{HTTP_HOST} ^www\.techiegyan\.com
     RewriteRule ^/index\.html$ http://www.techiegyan.com/ [R=301,L]
     RewriteCond %{HTTP_HOST} ^techiegyan\.com
     RewriteRule ^/(.*) http://www.techiegyan.com/$1 [R=301,L]
</VirtualHost>
 

Above is an example of configuration which may achieve following :

a. Link http://techiegyan.com will be redirected to http://www.techiegyan.com
b. Link http://techiegyan.com/index.html will be redirected to http://www.techiegyan.com
c. Link http://www.techiegyan.com/index.html will be redirected to http://www.techiegyan.com
d. Links like http://techiegyan.com/any_page.html will be redirected to http://www.techiegyan.com/any_page.html

Configuration above is achieving a common patter of URL which will be helpful for Search engine to index.

Most Commented Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

I love this site, so thank you

Thanks for your appreciation. Thanks for your comment and time.

Leave a comment

(required)

(required)