Rate this page

Flattr this

Redirect HTTP requests from one domain to another using Apache

Tested on

Debian (Etch, Lenny, Squeeze)
Ubuntu (Lucid, Maverick, Natty)

Objective

To configure an Apache webserver to permanently redirect all requests from one domain to another (which may or may not be hosted by the same machine)

Background

It is quite common for a website to be accessible through more than one domain name. Examples include:

One way to achieve this would be to use two or more virtual host configurations that point to the same document root. The main drawback of this approach is that search engines then see multiple copies of the content, which may cause it to rank less highly than it would do otherwise. Another issue is that it gives no indication of which is the preferred domain, so may result in a mixture of domains being used in backlinks.

A better method is to choose one domain as the canonical location of the website then use an HTTP redirection to send all other traffic to that domain. Search engines should then see only one copy of the content, and web browsers with an address bar should show the canonical URL once a page has redirected.

Scenario

Suppose you are running a website for which the canonical domain name is www.example.com. You wish to permanently redirect any requests for pages in the domain example.com to www.example.com.

Method

One way to perform a redirection is to use the Redirect directive provided by the mod_alias module:

Redirect permanent / http://www.example.com/

The first argument specifies the type of redirection to be performed. A value of permanent corresponds to a status code of 301 (Moved Permanently), meaning that future requests for the same URL can and preferably should be sent directly to the new location. This is the appropriate behaviour when redirecting to a canonical domain name, as in the scenario above. It would be equally permissible to specify the required status code numerically (replacing permanent with 301).

The second argument is the path to be redirected, in this case the root of the website. The redirection applies to any request that begins with this path. Redirecting the root therefore amounts to redirecting the whole website.

The third argument is the URL to which the user agent should be redirected if the path exactly matches the second argument. For paths that match the second argument as a prefix, the unmatched tail is appended to this URL. For example, the path /foo/bar.html would be redirected to http://www.example.com/foo/bar.html by the configuration shown above.

When redirecting an entire domain this directive would typically be placed in the configuration for the relevant virtual host, for example:

<VirtualHost *:80>
 ServerName example.com
 Redirect permanent / http://www.example.com/
 # ...
</VirtualHost>

However it can be used in any configuration context if required, including within an .htaccess file.

It is necessary for the mod_alias module to be enabled within the Apache configuration. Typically this will already have been done as part of the default configuration.

Testing

One way to test that the redirection is occuring is to use curl to fetch a URL from within the affected domain then inspect the resulting HTTP headers. For example:

curl -I http://example.com/

The relevant parts of the output are the first line, which contains the status code, and the Location header, which contains the replacement URL:

HTTP/1.1 301 Moved Permanently
Date: Wed, 18 May 2011 21:02:05 GMT
Server: Apache/2.2.9 (Debian)
Location: http://www.example.com/
Content-Type: text/html; charset=iso-8859-1

For completeness it would be desirable to test both the root of the domain and a path within the domain.

See also

Further reading

Tags: apache | http