Add www to Domain Using .htaccess

By David Edelman on Thursday, March 8, 2007
Filed under SEO

Is your website accessible at example.com and www.example.com? If so, then you're not optimized for search engines!

Think of every link your site receives as a vote. If you get five links to example.com and five links to www.example.com, you have two separate web pages with five votes. It would be much better if you could tell Google to use one of those pages and give it a ranking of ten. On an Apache server, you can using .htaccess and mod_rewrite.

Simply place this .htaccess file in the root of your site and change example to your web address:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]

RewriteEngine on tells Apache's mod_rewrite module to review each address and check it against the rules that follow.

RewriteCond tells mod_rewrite what pattern to look for. In this case, it's anything that starts with "example.com". The dollar sign tells mod_rewrite to store all information after that point for later use.

RewriteRule tells mod_rewrite what to do with the pattern it matched above (everything after the dollar sign). In this case, it says make the url "http://www.example.com/" and then append everything after the first (and in this case only) dollar sign here.

The R=301 part is important. This tells the search engine that the page has permanently moved from example.com to www.example.com and it should transfer all of the votes there. Without this directive, Google would assume it's just a temporary move.

For More Information

Apache's mod_rewrite Documentaton
An .htaccess Overview

Comments

I wrote about this in my blog here a few days ago: http://www.firelead.com/2007/02/21/improve-your-google-pagerank-with-one-easy-step/
What I reported is that Matt Cutts himself from Google said that you should tell Google what URL you prefer. The important distinction here, is that if you prefer the www over the non www then you should tell Google that in Webmaster tools by Google.

That is correct. If you're a webmaster and aren't using Google's Webmaster Tools, sign up! Unfortunately, at this point you can only tell Google that way. It does not help with other search engines.

There are more benefits to having one root url than just search engine ranking. First, it negatively effects page statistics. If I'm monitoring pageviews, I want to make sure that I'm getting accurate information. If users are visiting two different urls, it's a pain to pull that information together. Second, there's plain old aesthetics. I like that my URLs always look the same regardless of what you type.

But thanks for the comment!

Comments are closed for this entry.