Redirect Non WWW visitors to WWW version and vice-versa using .htaccess

Redirect Non WWW users to WWW version of Website
If you have a website at www.example.com and  want to redirect users from example.com to www.example.com you can do this using a htaccess 301 permanent redirection.
This method is both SEO friendly and user friendly.
Even visitors landing on example.com/file.html will be redirected to www.example.com/file.html.
We also have the code to redirect www.exmple.com visitors to example.com.

Why should you redirect ?

Search engines like Google,Bing and Yahoo! would see example.com and www.example.com as two separate websites. They recommend you pick one version you would like search engines to display and using a 301 permanent redirect is the best option.

Is this method SEO friendly.

A 301 redirection is the most SEO friendly way to redirect users from one address to another. This is search engine friendly and won't cause harm to your SERP rankings.
Even Google recommends this
If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.
You can read the whole article here
So you dont have to worry about SEO even google recommends this method.

htaccess redirection: the code

Force Non-WWW users

Code above will redirect users from non-WWW version to WWW version.
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Force WWW users

Code snippet above will redirect users from WWW version to Non-WWW version.
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Final words

Make sure that you change example.com with your domain name. You can also customize this code if you want to use this with subdomains.
Don't try to use both codes at same time, this will cause an error.
If this method worked for you then do share it with others too and, if you are getting something wrong lets have a chat in comments.

Comments

Popular posts from this blog

Show Full Post on Front Page of Ghost, Not Excerpt

Video Blogger Templates

5 Beautiful Dark Blogger Templates