Using .htaccess to Redirect www and non-www

Its really important for new blogs or websites to deal correctly with canonical URL’s. If not then you will end up with bad SEO resulting in search engine ban. So we will see how to redirect www and non-www.

What are canonical URL’s?

Canonical urls are the search engine friendly urls. According to Google they call this process as Canonicalization which is the process of picking the best url when there are several choices, and it usually refers to home or index pages.

For example, in the view point of humans these urls are the same:

But technically all of these urls are different. Search engines treat all these urls as entirely different. A web server could return completely different content for all the urls above. When Google “canonicalizes” a url, it tries to pick the best url from that set.

The Problem?

Since search engines considers all the above urls as entirely different, the content will still remain the same in all the urls. This may lead to duplicate content issue.

Solution: Use Permanent www/non-www redirects

Choosing whether to redirect domain to completely www or without www(non-www) will avoid duplicate content. This also help to avoid the possibility of split page rank.

Using .htaccess to Redirect www and non-www

Before we see how to redirect, first login to your hosting cPanel and open .htaccess.

Note: If you do not have any file with name .htaccess then create one blank text file with the extension .htaccess. Also make sure you have chosen “Show hidden files and folders” before opening file manager.

Redirect non-www to www

Copy this code to .htaccess file.

Replace domain.com with your domain name. I thought of explaining this, but it would become bit lengthy and boaring for others who do not want to know much about this. So i will create a tutorial about .htaccess and how to use this file.

Redirect www to non-www

Copy this code to .htaccess file.

Replace domain.com with your domain name. I use the 2nd code in my htaccess file to redirect all www request to non-www.

Let us know if you need any help with .htaccess files. Feel free to comment, it just takes seconds and makes us fell better.

7 Responses to “Using .htaccess to Redirect www and non-www”
  1. sreenu January 24, 2012
  2. jessefogarty February 3, 2012
    • Sudheer Ranga February 8, 2012
  3. Jake Carlson April 23, 2012
  4. Bryan October 21, 2012
  5. Anisa February 13, 2013
  6. Cricket Highlights February 6, 2014

Leave a Reply

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