All web sites can be reached from two URL addresses: one with the WWW and the other without the WWW. For example, you can reach this blog by typing in http://www.natewhitehill.com and also http://natewhitehill.com.
However, it is imperative that you pick one address and stick with it for maximum search engine results and PageRank. If, generally, you use the version with the WWW and someone links to your site and they do not include the WWW, the search engine spiders will index that link as a separate domain from the one with the WWW.
For example, my good buddy, Matt from Take More Risks, recently had 2 separate PageRanks for his blog. His blog URL with the WWW had a PR 3, while the URL without the WWW had a PR 2. In other words, Google indexed that same domain as 2 separate ones. Most likely, he could have achieved a higher PageRank if he had set his preferred domain earlier. If you don’t quite understand PageRank, read this article about the upcoming PageRank update.
Which Preferred Domain Should You Choose?
Well, first you need to pick which version you rather use. I chose to go without the WWW, so my preferred domain is http://natewhitehill.com. There is no one reason to pick one instead of the other – it is purely a matter of preference. I chose to go without the WWW because having a longer domain name (13 characters, in my case), I thought it would make it easier for people to remember the URL if they just saw the natewhitehill.com and not the www.natewhitehill.com. John Chow, on the other hand, chose to use the WWW. Just to reiterate, there is no definitive reason to pick one over the other.
Setting the Preferred Domain
After you choose your preferred domain, you must now create a 301 Redirect to forward one address to the other. If you are hosted on a Linux server, you do this by editing your .htaccess file. You may first need to create one and to do this you must:
1) Create a simple text file and name it .htaccess
2) Modify it with the proper code (details below)
3) Upload it to the root of your server
Here is the code you should use if you want people to redirect from non-WWW to WWW:
(for example, if someone types in http://domain.com, it will forward to http://www.domain.com)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Here is the code you should use if you want people to redirect from WWW to non-WWW:
(for example, if someone types in http://www.domain.com, it will forward to http://domain.com)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^.*$ http://domain.com%{REQUEST_URI} [R=301,L]
In both cases, make sure to replace “domain” with the URL of your domain.
By setting a preferred domain you are ensuring that Google and all the major search engines will only index links from one domain. This will help your PageRank the next time around and also you receive more weight in current search engine rankings.
If you have not set your preferred domain yet or have been putting it off, DO IT NOW. You will thank me later when you get a higher PageRank.
Done! Thanks for the heads-up, Nate. I appreciate the sound advice.
I’ve been thinking about this for a while but your post has spurred me into action. I’ve now set it on the server and then gone into Google Webmaster tools and set the preference to the same. Hopefully they won’t penalize me for changing this right before a PR update. Cheers.
Why yes, yes I have. I always use WWW. I have WordPress-MU setup on one website, and it doesn’t work well with WWW, so that’s the only one that isn’t WWW.
The same thing also applies to the default page for a site. The root, “/”, and “/index.php” or “/default.aspx” are considered unique URLs in the search engines. A similar rewrite rule can be added to 301 redirect all requests for the default document to the root of that folder.
I covered how to do this with a WordPress blog a little while back on my blog:
http://www.ryanmcdonnell.com/redirect-indexphp-to-with-wordpress/
The rule looks something like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule . / [R=301,L]
Hey Ryan – great tip! Thanks for bringing this up! 🙂
Great piece of advice. I did this 2 months ago when I realized I was able to do so.
Good advice. Done this a while back actually.
Very good suggestion. I use this type of code on all of my content sites also.
You would expect that since www and non-www sites display the same, a search engine would be able to know that. I can’t think of any time when http://www.example.com and example.com would ever be different.
Funny you should say that. Ja Kel has this very setup with his wife’s blog. It’s pretty unusual but it seems to work for them.
http://jakeldaily.com/two-blogs-one-domain-double-power/
Nate,
I notice that if I put www in front of this posts url … it redirects to your homepage … hmm. Wouldn’t it be better to redirect to the same address without the www? Not that I have a clue if it’s better or how you would do it … just some fuel for the discussion here
Hey Shane, yeah I just noticed that. Frankly, I have no idea how to fix it so that if someone does have the full post URL and types it WWW it redirects to the post without the WWW. Thankfully, most people will copy and paste a post URL instead of typing it out long hand. Interesting point, however.
I’m just thinking that if you link to your own posts or other people have links to certain posts using www, and you change your preferred domain to non-www, those links are now wrong.
And it’s only a problem if you change this mid-stream in a site’s life after some links have been established.
Guess I’ll look into it a bit more and make a decision,
thanks Nate
You are correct that if I change my site back to WWW the links will be dead. I guess I’ll just have to stick with non-WWW for my lifespan. Let me know if you find a way around the redirect for the individual posts.
Can you post the contents of your .htaccess file? I’m thinking there might another rule that is causing this behavior. I’m using the same rule and individual posts are being redirected properly.
My .htaccess looks like this:
Options +FollowSymLinks
# Redirect @ to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.ryanmcdonnell\.com$ [NC]
RewriteRule ^(.*)$ http://www.ryanmcdonnell.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule . / [R=301,L]
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
This may help, a little different…
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
RewriteRule ^(.*) http://www.yourdomain.com/$1 [L,R=301]
Thanks for the code, Ryan. That is very helpful.
will this work if i do a mod rewrite to foward all digg traffic to a mirror?
301 redirects work for directing any URL to any oher URL.
A 301 redirect will direct all the weight of the backlink to the target domain. Although there may be a time lapse before the weight is transfered, it will transfer. This not only works within a site, but also if you switch domains. Think of it as forwarding your mail to your new apartment.
Forget about adding code to your .htacces and install this simple plugin: http://txfx.net/code/wordpress/enforce-www-preference/
Thanks for the heads up on the plugin, that will simply things quite a bit for those not so technically savvy.
best bet not to mess with .htaccess especially when the program is writing to it.
Natron,
Awesome.
I try to keep plugins to a minimum, but I don’t know too much about .htaccess. I tried both your code and the plugin. They both work perfectly so I’m going to go with the plugin so I don’t overwrite the code accidentally.
And thanks for starting the discussion Nate!
I always wondered if there was any logic behind dropping the www.
I would think that appending the “www” would be the best bet as most will link with the “www” added anyway, you have less links to 301 redirect. I hope Nate responds with his reasoning.
Only reason I got rid ot the WWW was so that it would be easier for people to visually remember my name from looking at the URL without all the WWW’s.
I prefer the fully qualified domain with the “www” as many editors will associate that as a link. That will be the only real reason I see.
this rocks.
Curious if my domain is in a subdirectory, for instance I have a blog installed in a blog folder like http://www.mydomain.com/blog do I have to change the htaccess file to see the subdirectory or is it fine as is?
You would need to place a .htaccess file in the root to redirect any traffic to the subdirectory. In the subdirectory you would also need a directive as given to append the “www”
Thanks thought I am not exactly sure what you mean by in the subidrectory you would also need a directive as given to append the wwww.
Are you saying that I need to add the same code to another htaccess file in the subdirectory as well? Forgive my rookieness
Damn I wish I read all the comments earlier. That plugin works great for redirecting and that would work just as good as manual. Still like to know how to get it to work manually. I tried it but it would only redirect my main page in the root, anything that was in my subdirectory wouldnt do anything
The problem persists. I writed down code you proposed and uploaded it to the server with *.txt format. however, now, when trying to reach my site with WWW, 404 error accures… What shall I do?
Exactly the info I needed and in plain English. Thanks!
Once again, I forgot to set the preferred domain. I entered my website into the WebsiteGrader and got a terrible ranking. Since then, I have been using ShoeMoney Tools plus other resources to improve my overall SEO portfolio. Thanks for the great post, Nate. I have to search Google and grab the code for every site I start! LOL
Is this code or file will work on my static site?
Great tip, thanks – note that if you use WordPress the rewrite rule may already be set, so probably best not to do it twice : ) .. just look in the htaccess and you will see.