Use mod_rewrite to ensure use of full domain name ( force www. )
With all of the SEO tips out there it can be easy to overlook some of the more simple but powerful techniques. One of these techniques is simply ensuring that your website is forcing the use of the "www." in your hostname. This ensures that your pages are being indexed at your full domain name. Why does this matter? Well basically it is about staying consistent for the search engines and your users.
As an example, open up a new browser window and enter "http://moedesigns.com" into the address bar. Once you hit enter, your browser visits my server at http://moedesigns.com and my server tells your browser to redirect to http://www.moedesigns.com. Not only does it check for the existence of the www, but it checks against anything that is not "www.moedesigns.com". So not only will http://moedesigns.com be redirected, but so will http://testing.moedesigns.com or http://anything.moedesigns.com. This all happens in less than a second so you normally won't notice anything except the URL automatically changing in your address bar.
Please note that this tutorial requires you to be running Apache Web Server. IIS is different and this tutorial will not work for it.
In the root of your web folder you should have a file called: .htaccess ( note the . ). If you do not have one, you can use notepad to create a text file on your computer and copy the following code into it. Once that is done you can simply upload the file to your web server and rename it to .htaccess.
Be sure to change all occurrences of "moedesigns" to your domain.
This is the mod_rewrite code I use to force the use of "www" when visiting "www.moedesigns.com":
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.moedesigns\.com
RewriteRule (.*) http://www.moedesigns.com/$1 [R=301,L]
</IfModule>
Explanation:
Line 1: <IfModule mod_rewrite.c>
This line simply checks to ensure that the mod_rewrite module is loaded into your apache configuration.
Line 2: RewriteEngine On
This tells apache to start using the mod_rewrite engine.
Line 3: Rewritecond %{HTTP_HOST} !^www\.moedesigns\.com
This line sets a "Rewrite Condition". This tells mod_rewrite what to look for. In this case we are looking in variable %{HTTP_HOST} and we are looking for: !^www\.moedesigns\.com
The ! means "does not equal" and the ^ means "beginning with". The \'s in front of the . are called "escape characters". All that they are doing is telling mod_rewrite that the period after "www" and the period before "com" are not command characters like the ! or the ^.
So if we put the entire statement together, we are looking to see if the HTTP_HOST variable "does not equal" a string "beginning with" www.moedesigns.com. So now lets move onto the next line!
Line 4: RewriteRule (.*) http://www.moedesigns.com/$1 [R=301,L]
This is the final step! Setting the "Rewrite Rule". In the previous step we told mod_rewrite what to look for, any string that does not match www.moedesigns.com. Now we are going to tell mod_rewrite what to do if it finds what it is looking for. We do this with a "rewrite rule". Our rewrite rule on this one is simple, redirect to the correct hostname (www.moedesigns.com) but keep all the arguments ($1). This is all we need but as I am sure you have noticed there is still [R=301] at the end of the line. This tells the browser, or search engine spider, that this is a permanent 301 redirect. If a spider reaches this URL and receives a 301 redirect, it will know that the content is moved and will index the page using the new URL. It should also update the URL it has stored for that page with the new link.
Line 5: </IfModule>
This simply ends the block of code that is ran if mod_rewrite is enabled.
Checking to ensure that mod_rewrite is enabled:
If you are not sure whether or not your have mod_rewrite enabled, you can check your httpd.conf for the following two lines:
----
LoadModule rewrite_module libexec/apache/mod_rewrite.so
AddModule mod_rewrite.c
----
If those two lines are in your configuration then you are good to go. Apache comes out of the bag with mod_rewrite enabled so this should not be necessary.
Well that should just about do it, if you are interested in URL aliasing or some of the other features of mod_rewrite you can visit the Apache mod_rewrite documentation page at: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
If you feel I have missed anything or possibly made an incorrect statement, please use the comment system to leave some feedback.
