How to Force www in URL Using .htaccess

January 18, 2012 .htaccess Comments Off

How to Force WWW in a URL

I hear a lot about Google and duplicate content as well as Google Canonical problems. That is, when you have your site accessible both under yourdomain.com and www.yourdomain.com. To avoid the issue of duplicate content you can use the following lines in your .htaccess file to force only the www version of your web site instead of the non-www version. Accessing them both can be a problem in Google’s opinion.

The .htaccess code to force www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

The above code will cause your URL’s to always show the www version of your website. It is a great idea to do this. You can also force the non-www versionby placing the following code into the .htaccess instead of the above code.

The .htaccess code for non-www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

About author:

Sorry, you can not to browse this website.

Because you are using an outdated version of MS Internet Explorer. For a better experience using websites, please upgrade to a modern web browser.

Mozilla Firefox Microsoft Internet Explorer Apple Safari Google Chrome