10 May 2011

301 redirect non www urls to www using htaccess

In the search engine optimization url canonicalization issues are very common. What is url caonicalization? Basically url canonicalization means that two or more of your urls are having duplicate content. An example of such urls is as follows:


http://example.com
http://www.example.com

Both of the above mentioned urls will have the same content therefore canonicalizing each other. To resolve this issue we 301 redirect one of the url to other. Mostly we 301 redirect non www url to the one with www.

This can be done simply by adding the following code in the .htaccess file:


###########FOR example.com to www.example.com###########
RewriteEngine on
Rewritecond %{http_host} ^example.com [nc]
Rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
#########################################################

For any queries or questions, feel free to comment.

No comments: