This article describes how to make a redirect from the www version of your site to non-www version. So, when someone types in http://www.mydomain.com – it will automatically resolve to http://www.domain-name.com.
For example, a popular site StackOverflow works as follows: when you type with www as http://www.stackoverflow.com/ – it automatically redirects you to non-www URL: http://stackoverflow.com/.
Search engines don’t automatically assume that both the ‘www’ and ‘non-www’ version of a domain are one and the same. This is why it’s important to tell the search engines which domain you prefer through using the .htaccess file.
Using .htaccess to Redirect
.htaccess is a text file you would upload in the root directory of your site. You create the file in a text editor like Notepad, and add the following code:
[codesyntax lang=”php”]
Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^www.domain.com [NC] rewriterule ^(.*)$ http://domain.com/$1 [R=301,NC,L]
[/codesyntax]
or use a more general version:
[codesyntax lang=”php”]
Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L] [/codesyntax]
This will ensure that if you type in ‘www.mydomain.com’’ it will automatically redirect your visitor to the non-www version on your site.
* Note: it does a 301 (permanent) redirect to http://mydomain.com/
*Note: this will only work if your host works with Linux servers with Apache mod-rewrite. If you try the above and it’s not working, ask your host if it is possible.
Telling the search engines that one version (the www, or non-www version, doesn’t matter which), is your primary domain, will ensure that all your valuable incoming links are being ‘counted’ towards the same domain and providing the greatest SEO benefit to your site. It’s important to choose a version and stick with it. Make sure when you link internally you are consistent, using either http://example.com, or http://www.example.com. Be sure to redirect to the version you’ve chosen, using a 301 redirect in your .htaccess file. In your Google Webmaster account, indicate which version is your main one.