How to Redirect URLs from Your Root Domain to the WWW Subdomain and Vice Versa

Add or Remove the WWW Prefix from Your Domain Names in Your .htaccess

How to Redirect from Your Root Domain to the WWW Subdomain and Vice Versa Using mod_rewrite

by Christopher Heng,

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 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.

The Problem Explained in Greater Detail

Having your site referred to as both and 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 "" will automatically type "" 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 and, 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" if you use a different variant of 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 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 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.

  1. 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.

  2. 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.

  3. 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.

Steps to Redirecting Your URLs Using .htaccess

Before you begin, please note these things:

If you prefer your website to be referred to as (say), 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, 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.

How to Automatically Add the "www" Prefix to Your Domain Names in URLs

To redirect (say) to, add the following code to your .htaccess file.

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

The above code causes the server to check that the domain name portion of the URL is If it is, the visitor will be sent to instead.

If you also own, say, and/or some other domain, and want them to be redirected to as well, you may prefer to use the following code instead:

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

This second set of rules checks to see if the URL has a domain name portion of If it doesn't, the visitor is redirected to 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 "" 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. And if your website uses HTTPS instead of HTTP, modify the "http://" portion so that it reads "https://".

Note that the second set of rules has some side effects:

  1. 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,

  2. 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.

  3. 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) 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.

How to Remove the WWW Prefix from Your Domain Names Using Apache's mod_rewrite

To redirect any URL starting with (say) to, add the following code to your .htaccess file.

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

The above rules checks if the domain name component in a URL contains If it does, the visitor is sent to instead.

To use the rule, change all instances of "" 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. In addition, remember to change the "http://" to "https://" if your website uses HTTPS.

Final Step: Upload and Test the .htaccess File

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.

Copyright © 2008-2018 by Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Articles

New Articles

How to Link to This Page

It will appear on your page as:

How to Redirect from Your Root Domain to the WWW Subdomain and Vice Versa Using mod_rewrite

Link to Us
No Spam Policy
Privacy Policy
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Web Hosting
Perl / CGI
.htaccess / Apache


Free webmasters and programmers resources, scripts and tutorials Free How-To Guides
Site Design Tips at
Find this site useful?
Please link to us.