Automatically redirect non-www requests to www for all URLs and All Domains
Redirecting non-www to www for ALL of your domains.
This guide will quickly go over why you should not use both www and non www versions of your domain, but choose only one, and how this affects your search engine rankings for Google.
Why you should only have www or non www, but not both for your website and it’s search engine optimization.
Many people have made their website accessible both through http://www.domain.com as well as just http://domain.com. This is not generally a good idea for your SEO (search engine optimization). You’re asking “Why?”, and I will explain. Google “king of search engines” and the only search engine that really should be important to you, treats subdomains as separate domains than the main domain.
In Google’s eyes; www.domain.com, domain.com are separate websites. If you are thinking “Great! My website is then going to be listed twice at Google!”, you will now need to reconsider, being wiser.
Google looks for “duplicate content”, or content which appears in more than one location. If you have the same blog entry written twice, in different locations on your website, you will lose some ranking for this. If you have copied text from another website, Google will try and determine which site first appeared with that original content, and will rank the rest a bit lower.
So what’s the point of all this www and non www talk?
If Google treats www.domain.com and domain.com as separate sites, and each one actually displays the same identical content, Google will rank both domain and www. subdomain lower, due to duplicate content.
How we do it where I’m from…
If you want to have all your URLs have a “www” in front of them then paste the following code inside of your “.htaccess” file.
If you don’t have a .htaccess file, create a new text document, paste in the below code, and upload it to the directory on your web host just above all of your domain name’s folders. Once uploaded, rename it to be “.htaccess”.
RewriteEngine On
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Good article and a great reminder. Thanks!
Just wanted to remind folks that it’s a great idea to canonicalize URLs at all levels, too… if there’s multiple ways to reach your content (e.g. http://www.tannerwilliamson.com/?p=547 or http://www.tannerwilliamson.com/2009/09/automatically-redirect-non-www-requests-append-www-every-url-domains/ ), ensuring that all forms of the URL redirect to the “correct” address is key.
How to do this varies from software to software, so some research might be involved, but it’s worth it. Last thing we need to do is compete with ourselves ;-)
@Ryan Tucker, That’s an excellent suggestion. For WordPress users, I highly recommend a plugin called “Redirection”. It provides an incredibly powerful interface for managing all 404 Errors, both at the Apache HTTPD level, as well as at the WordPress system level ( in case you already have a plugin which intercepts 404′s and turns them into normal header returns for SEO purposes). It will allow you to log all of these errors automatically, then provides a one-click 301 rewrite / redirect rule for correcting any bad incoming links. It will even automatically update any URL’s which have been renamed and put in place the appropriate 301 mod_rewrite and redirect rules. And BTW You’re welcome for the article.
Hi, Thanks for the rewrite rule.
Is there a way to exclude the sub-domains?
For example: blog.domain.com should not have a www in front of it.
Any suggestions?