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, thesitewizard.com

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.

The Problem Explained in Greater Detail

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 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 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 ("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.)

Prerequisites

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.

Steps to Redirecting Your URLs Using .htaccess

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 ("period") 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 a plain text editor and not a word processor 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 domain name portion of 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 domain name portion of www.example.com . If it doesn't, the visitor is redirected to www.example.com . In other words, any web address with a domain name component 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. 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:

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, www.example.com ). 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 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 domain name.

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 domain name component 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. 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 https://www.thesitewizard.com/.

thesitewizard™ News Feed (RSS Site Feed)

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.

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

Popular Articles

How to Link to This Page

To link to this page from your website, simply cut and paste the following code to your web page. <a href="https://www.thesitewizard.com/apache/redirect-domain-www-subdomain.shtml" target="_top">How to Redirect from Your Root Domain to the WWW Subdomain and Vice Versa Using mod_rewrite</a>

It will appear on your page as:

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

Copyright © 2008-2018 by Christopher Heng. All rights reserved.

thesitewizard™, thefreecountry™ and HowToHaven™ are trademarks of Christopher Heng.

This page was last updated on 24 April 2018.