Force remove the ‘www’ from domains

This is very essential for webmasters, to avoid the search engines’ duplicate penalty. It will also make your links look better.

Simply, Google and other search engines would consider the two following domains a duplicate of each other and apply a ‘duplicate content penalty’:

Duplicate penalty:

This penalty is applied by Google and possibly other search engines when content found on your website is largely the same as what is found elsewhere on your site or on other websites across the internet.

The penalty applied is lowering the page rank of both websites; this means not showing on top of the search results.

Choose one of the domains:

You can either force the ‘www’ to exist if the user doesn’t type it as a part of the URL, or you can force remove it even if your visitor typed it in the address bar. it can work either way as long as you choose to apply one of the two cases.

For me, I like to remove the ‘www’ .. I believe it looks better, but you can choose whatever suits you better. The idea is to permanently redirect the visitors to whatever domain name you choose, and since the APACHE servers are the most widely used web-server, I will be discussing applying the permanent redirect through the .htaccess file.

The code:

# this is to go in the .htaccess in the root of the web_directory

RewriteEngine On
rewritecond %{http_host} ^www.example.com$ [nc]
rewriterule ^(.*)$ http://example.com/$1 [r=301,nc]

Copy the previous code, and paste it in the .htacess file located in root directory for the website where you hosting your files.

Do not forget to use your own domain instead of “example.com” in my code. ;-)

What this actually do:

This will tell both the browsers, and the search engines, that the website (that starts with ‘www’) has been permanently moved to the other domain (in this case, the one without the ‘www’).

Hint:
You can alter the code to enforce the existence of the ‘www’ in the URL. try it and post in the comments if you need any help.

  • Shelsea E.

    My blog is hosted on Blogger but I recently changed its URL to .com only.
    The thing is, browsers won’t find my blog if I don’t type in www.

    I have a few questions.
    1. Does this method work for blogs/sites hosted on Blogger?
    2. If yes, where do I find the root of web directory to paste the code?

    I appreciate your help.

    • http://www.alybadawy.com Aly Badawy

      Thanks Shelsea for leaving the comment.

      Well, first, as for problem of people can not reach your website unless the type in the “www”, I believe this is because of the way your DNS for the domain is configuerd. Make sure you have CNAME records for both the domain.com and http://www.domain.com! A good way is also using the wildcards like *.domain.com in the DNS; this will make sure that requests for both the www and without the www to be redirected to the blogger server.
      I just did a whois on your domain and see that you only have a CNAME for http://www.yourdomain.com redirect to ghs.google.com, just add another CNAME for yourdomain.com redirecting to the same ghs.google.com and this should do the trick ;-)

      As for applying my method of removing the ‘www’ from the URL when a request is done using the ‘www’, unfortunately, this will not work in the case of Blogger due to the fact that you do not have access to web directory; as this is hosted by Google’s servers.
      However, I believe that blogger’s settings should allow you to choose the custom URL and enforce that by default in the configurations.