How to 301 redirect non-www URLs to www URLs using htaccess

If your website can be accessed by two different URLs then your website could be penalized for duplicate content by the search engines which would be costly in terms of your rankings. For e.g. if your website can be accessed by http://www.exampleurl.com and http://exampleurl.com then Google may deem it as duplicate content.

Google provides a method known as setting up your “Preferred Domain” in your Google Webmaster Tools. Once you have done that then Google would crawl, index and rank your website using the Preferred Domain however they still recommend that you should 301 redirect all other URLs to your Preferred Domain URL so that you can use any link juice on those links and improve your website ranking.

Add the following code in your .htaccess file which will 301 redirect all non-www URLs to your www URL.

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Comments?

Leave a Reply

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