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

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

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

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.

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

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

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

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

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.

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.

Addendum: For the Confused — Domain Names and Subdomain Names

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.

Copyright © 2008-2014 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/.

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

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.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce this article in whole or part, in any form, without obtaining my written permission.

Related Articles

New Articles

Popular 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





Home
Donate
Contact Us
Link to Us
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

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