Most websites stick to one form of their domain name when they refer to their own site, be it the plain domain name, like
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
www.example.com is not a problem of
consistency. Neither is it an issue that affects branding, since most humans looking at those two
names 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
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 ("realize" in
US English) 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 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 the page specified 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 commercial web hosts are more likely to have it than 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" in
US English) and has
no additional extension (for example, it does not end with ".txt").
.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)
the following code to your
The above code causes the server to check that the domain name portion of the URL is
example.com. If it is, the
visitor will be sent to
If you also own, say
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:
This second set of rules checks to see if the URL has a domain name portion of
www.example.com. If it doesn't, the visitor is
www.example.com. In other words, any web address with a domain name component other than
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
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 domain you specify above (eg,
In the same way, you will no longer be able to access your site by 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
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 domain name.
To redirect any URL starting with (say)
example.com, add the following code
The above rules checks if the domain name component in a URL contains
www.example.com. If it does, the visitor is
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
file already there, download it to a backup folder on your computer for safekeeping. In fact, if there is an existing
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.
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 https://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 or distribute this article in whole or part, in any form.
It will appear on your page as: