Saturday, October 1, 2011

Redirect non www to www url

It is very important from SEO point of view for a site to open from single url.
If you want that your site should only open with www preceding the url you should
just copy and paste the code below in your .htaccess file and you are done.For
example if a site opens with 'abc.com'  and  also with 'www.abc.com'  and you want
that it should always open with 'www.abc.com' and if one tries to open with
'abc.com' it should redirect it to 'www.abc.com'.

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

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

1 comment: