As well as for redirecting the non-www version of your site to the www version in the easiest and best way.
There are other ways to do it, such as through your domain registrar, but they create a variety of problems.
This article applies to Linux/Unix/FreeBSD hosting (which is the majority of hosting these days), and is written for the mildly technical, to webmasters.
Lets say you own a domain: MySiteRocks.com
but because you think people will mis-spell “Rocks” you also own: MySiteRox.com
WHY IT’S IMPORTANT
It helps the search engines and real site visitors in a BIG way. Want to create a nightmare of a problem for search engines? Then ignore what I am about to say.
THE PROBLEMS
Lets look at it from a visitors perspective first.
In most cases, this redirection is setup so that when the visitor types in the mis-spelled version of the domain, they stay on that misspelled version as they navigate your site. In other words, their address bar looks something like this: http://www.mysiterox.com/contact.html
What would be best is if the address in the visitors address bar, automatically updated (redirected) to the correct spelling of the site. Not only because they will learn how to go the correct website in the future, but also because if they sent you an email to the misspelled version, you would not ever receive it. It also decreases the chances of other more complicated problems related to the website as well. I see these problems constantly.
Now, let’s look at it from a search engine perspective. This applies both to extra domains if you have them, as well as something you should do even for a single domain. And that is to have:
http://mygreatsiterocks.com
automatically redirect to
http://www.mysitereallyrocks.com
You could even do it the other way around (www points to non-www version) although that is uncommon to do it that way. Search engines are a bit dumb sometimes, and they seem to sometimes fail to realize that those two addresses are indeed the same site, believe it or not. Its a problem called cannonicalization.
The solution:
You need to setup what is known as a 301 permanent redirect. There are other redirect types out there that are bad for a site when it comes to search engines (generally speaking) such as a 302 temporary redirect or a Javascript redirect. This redirect not only applies to extra domains if you have them, but also redirecting the non-www version of your site to the www version.
Regarding the extra domains, some hosting companies will have tools to do this, but it is common for them to use what is known as a ServerAlias directive in your hosting configuration file. This will have alternate domains that you want pointed to your main domain.
After you have ensured that is the case, then you can create and upload an .htaccess file in your site’s document root if there’s not already one there. Your document root is the folder on your hosting account that contains your website files and is often called public_html, www, or something similar.
In that .htaccess file, you can add this:
RewriteEngine On
# REDIRECTS NON-WWW TO WWW VERSION OF SITE
# AND REDIRECTS ALTERNATE ALIASED DOMAINS (SUCH AS TYPOS) TO REAL DOMAIN.
RewriteCond %{HTTP_HOST} !^www\.mysiterocks\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^(.*) http://www.mysiterocks.com/$1 [L,R=301]
This will not only redirect non-www to the www version of your site, but also redirect other domains you have pointed to this website. Note: This uses what is technically known as mod_rewrite, and is a feature of Apache web server.
By the way, if you are placing links to your site from other places on your site, or getting links from other sites, always use the same link. e.g. If you are usinghttp://www.mysiterocks.com then always use the www. Also don’t put index.html at the end unless thats how your homepage appears. You can create a rule to redirect index pages if you want, but I have rarely seen a real need to.