Most websites stick to one form of their domain name when they refer to their own site, be it the plain domain name, like example.com,
or the www form, like www.example.com. Unfortunately others linking to you don't always follow your preferred style. If some
sites link to you with just the domain name, and others link using the www subdomain, then your site may face issues with the search engines.
This article shows you how you can work around the problem by automatically redirecting all URL requests of one form to the other in a
search engine friendly way.
Having your site referred to as both example.com and www.example.com is not a problem of
consistency. Neither is it an issue that affects branding, since most humans looking at those two
hostnames will
automatically assume they are referring to the same thing. In fact, most humans, when told to go to a domain
like "thesitewizard.com" will automatically type "www.thesitewizard.com" in their browsers, since this is the way
the majority of the websites on the Internet work, and they know it. So the problem isn't really with human beings at all.
It is a problem with search engines.
When you have two different addresses pointing to the same page, like www.example.com/offers.html and
example.com/offers.html, many search engines (or so we are led to believe)
will treat those two URLs as two separate pages. When you, as a human, see those two pages and notice they are identical,
you will automatically realise (correctly) that they are actually the same page. Apparently, the search engines do not
make this assumption, and will regard those as different pages with duplicate content.
The problem with duplicate content is a topic I have written on before, so I will not repeat myself here. You can learn more about it from my article How to Create a Search Engine Friendly Website.
The way to deal with it is to redirect all pages using one form of the web address ("URL") to the other form, using what is known as a "301" (or "permanent") redirection code. When the search engines see the redirection along with the 301 code, it will realise that that particular page has been permanently relocated to another address. (Don't worry if you don't understand this paragraph. It's just an overview of what you'll be doing later in this article.)
For the method described in this tutorial to work, the following conditions must be true.
Your website must be hosted on an Apache web server. In general, if your site is hosted on a Linux, FreeBSD, or Unix-type server, it is probably using the Apache server. If your site is hosted on a Windows system, this is unlikely to be the case. One way to find out for sure is to ask your web host or check the documentation on their website.
Your web host must allow you to override your web server's configuration using a .htaccess file. Again,
this information is available from your web host.
Your web host must have installed and enabled the mod_rewrite Apache module. Not all web hosts provide this, although the commercial web hosts are more likely to have it than the free web hosts. If you are using the same web host as I am, the mod_rewrite module is available for use with every package.
Before you begin, please note these things:
In the following sections, you will be creating or modifying a file named .htaccess on your web server. Note carefully
the name of the file -- it starts with a full stop (or "period" if you use US English) and has no additional extension (for example, it does not
end with ".txt").
The .htaccess file is a simple ASCII text file, so to create it, you should use an
ASCII text editor and not a wordprocessor
like Word, Wordpad, Write, OpenOffice, etc. If you use Windows, you may use Notepad, but be sure to read my article on
How to Save a File with Notepad Without the
TXT Extension otherwise Notepad will invisibly add a ".txt" extension behind your back. You won't be able to spot this extension
in Windows Explorer in the default Windows configuration.
You should also standardize all the links on your website to use the form you want. For example, if you want www.example.com, make sure that
all your internal links use that form (or just use relative links).
Those who have created a site map using the search engines' Sitemap Protocol, should standardize the links there as well.
If you prefer your website to be referred to as (say) www.example.com, with the "www" prefix,
read the section on how to transparently add the "www" prefix to
your domain name. However, if you want your site to be referred to as example.com, without "www",
skip to the section on how to remove the "www" prefix
from your domain name.
Incidentally, it doesn't matter which you choose. The main thing is to choose one and stick with it. It's probably best to choose the form that is most widely used by other sites to link to you instead of randomly picking one.
To redirect (say) example.com to www.example.com, add
the following code to your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
The above code causes the server to check that the hostname in the URL is example.com. If it is, the
visitor will be sent to www.example.com instead.
If you also own, say example.co.uk, example.de and/or some other domain, and want them to be redirected to
www.example.com as well, you may prefer to use the following code instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This second set of rules checks to see if the URL has a hostname of www.example.com. If it doesn't, the visitor is redirected
to www.example.com. In other words, any web address with a hostname other than www.example.com will cause
a redirection to take place.
Whichever set of rules you use, change all instances of "example.com" to your actual domain name. For the line beginning with
RewriteCond, add a backslash ("\") before each of the full stops (or "periods") in your
domain name.
Note that the second set of rules has some side effects:
Some people park new domains they buy at
their current site, pending the development of a new website. If you do so, anyone accessing those parked domains will be
redirected to the hostname you specify above (eg, www.example.com).
In the same way, you will no longer be able to access your site at its IP address without being redirected. This is probably not a big issue, but it's good to know.
Some web hosts allow you to host multiple domains and subdomains on the same web account, with those domains redirected to a subdirectory of your
main domain. For example, if the files for your domain (say) eg-domain.com can also be accessed as example.com/eg-domain/,
this paragraph probably applies to you. Such web hosts may use mod_rewrite to implement support for these additional domains or subdomains. If
you then also use the second set of mod_rewrite rules on your URLs, you may get
404 File Not Found errors for URLs in your extra domains/subdomains,
depending on the end result of the combined rules. In such cases, it may be safer to use the
first set of rules instead, since it very specifically targets only a single hostname.
To redirect any URL starting with (say) www.example.com to example.com, add the following code
to your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
The above rules checks if the hostname in a URL contains www.example.com. If it does, the visitor is
sent to example.com instead.
To use the rule, change all instances of "example.com" to your actual domain name. For the line beginning with
RewriteCond, add a backslash ("\") before each of the full stops (or "periods") in your domain name.
Once you've finished creating your .htaccess file, upload it to your website. You should upload it to the top web
directory of your website, that is, the same folder where you place your website's home page. If there is an existing .htaccess
file already there, download it to a backup folder on your computer for safekeeping. In fact, if there is an existing .htaccess
file, you should download it as a first step and add the lines given above to the end of the file instead of simply replacing it with your own
file. But always keep a copy of the unmodified original as a backup.
After uploading your file, test your website. This is very important step whenever you are uploading a new
.htaccess file, since any mistake in that file can render your entire website inaccessible. If this happens,
just delete the bad .htaccess file. If your site previously had a .htaccess file, restore your backup of
that old file (just re-upload it). Never assume the rules will work or that you didn't make any errors.
Even the slightest error can take down your site.
For those who don't know how to upload a file, please see my article on How to Upload a File to Your Website Using the FileZilla FTP Client.
A domain name is a name like "example.com, which you buy from a domain name registrar. Subdomain names have a prefix added to the domain name, like "www.example.com" or "ftp.example.com". Normally, when you host your domain with a web host, the latter will create a number of subdomain names for your domain, like "www.example.com", "mail.example.com", "ftp.example.com" and so on, each with a different function. Domains and subdomains are often assigned to a particular address and machine on the internet. Each of these addresses/machines have at least one hostname, which may be "example.com" or "www.example.com", etc. Different hostnames may even share the same address and computer.
However, for the purpose of this article, you don't really have to understand all that. In fact, in order to simplify things,
I have sometimes used domain name and hostname somewhat interchangeably (as most people do on the Internet). For the purpose
of this article, it is sufficient to know that in a web address like
http://www.example.com/somefile.html, the www.example.com part is the hostname component.
Likewise, in a URL like http://example.com/somefile.html, example.com is the hostname.
Copyright © 2008 by Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from http://www.thesitewizard.com/.
If you find this article useful, please consider making a donation.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at http://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce this article in whole or part, in any form, without obtaining my written permission.
It will appear on your page as:
How to Redirect from Your Root Domain to the WWW Subdomain and Vice Versa Using mod_rewrite