Redirecting www To non-www And Vice Versa

Every domain and sub domain can be accessed with two different url. Say you have a website example.com. Then it can be accessed by using urls example.com and www.example.com. This is true in case of sub domains also. Say if you are having a website as blog.example.com then that website can be accessed by two urls, blog.expmle.com and www.blog.example.com. Even though there are two urls, the content on the both url will be same. According to user point of view, having two different urls for same content will not matter much. But according to SEO point of view, having same content on two different urls is a big problem for your website.

According to SEO, the same content should never be available on two different urls (especially www and non www). This may cause problem of duplicate content and may result into lower SEO rank. To deal with such situation and to avoid duplicate content you have to redirect one url to another.

Now you have two options, one you can redirect all www urls to non-www one or you can redirect all non-www to www one. Depending upon your need you can choose the option. But it is always suggested to redirect all www urls to non-www one. This will lead to shorter urls as the www part will get trimmed.

Setting up redirection

This is quite simple. First you have to locate your .htaccess file on your server. You will find this file in your root folder of you site or subdomain. If it is not present then create one. Open it and add following code in it.

If you want to redirect all www to non-www ones, add following code (best choice)

# Rewrite "www.example.com -> example.com".

<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

If you want to redirect all non-www urls to www one, add following code

# Rewrite "example.com -> www.example.com".

<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

That’s all. Save that .htaccess file and you are done with it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.