As some of you may already know, search engines treat “www” (e.g. http://www.blogopreneur.com) and “non-www” (e.g. http://blogopreneur.com) domains as totally different domains, until Google allowed you to set a preferred domain to use in your the Google Webmaster Center.

Preferred Domain Setting on Google Webmaster Center

So it is important to resolve this canonical domain issue if you want to market your blog, or website – so you don’t risk duplicate content (though I believe search engines today should be smart enough to see this potential problem) and so that your marketing efforts will be focused on one URL.

In most of the domains that I administer, I usually redirect all my “non-www” traffic to my “www” domain. But today I was researching how to do the opposite, since “www” is a subdomain afterall, and the “non-www” one is the real domain. I searched and I found this good resource on Webmaster World.

# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Redirect if example.com (case-insensitive) to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Redirect if NOT example.com (exactly) to example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]

# Redirect if www.example.com (case-insensitive) to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

Now we can do whatever we want! In particular, if you want to redirect all other subdomains to your root domain (not only your www one), you can use the third paragraph of code. Neat!