Linode Library Home
Linode Library RSS Feed
Home :: Web Servers :: Apache :: Configuration
Print View View Source

Redirect URLs with the Apache Web Server

Published: by

When HTTP resources, or web pages, change locations it is often important to provide some means of alerting users that these resources have moved. HTTP provides a number of "redirection" codes that can be used to facilitate this process, by communicating with he client application without interfering on the users experience.

Apache provides a number of "redirect" configuration directives that allow administrators to specify resources in the configuration file to redirect to another URL. When a request is redirected the server returns a result for that request that instructs the client to initiate a second request for the target resource's new location.

Redirections can tell the client that the requested page has been moved temporarily or permanently. Apache provides tools to easily support these functions. This guide covers the Redirect configuration directive, explains how to set various redirect options, and shows how to redirect classes of requests for resources to new locations.

This guide assumes you have a working installation of Apache and have access to modify configuration files. If you have not installed Apache, you might want to use one of our Apache installation guides or LAMP stack installaiton guides to get up and running first. If you want a more thorough introduction to Apache configuration, consider our basic Apache configuration and Apache configuration structure documents.


The Redirect Directive

The Redirect configuration directive can be located in "main" server configuration files, but we recommend that you keep them in your virtual hosting entry or directory blocks. It is also possible to assert Redirect statements in .httaccess files. Here is an example of a Redirect directive:

File excerpt:Apache configuration option

Redirect /squire

If no argument is given, Redirect sends a temporary (e.g. 302) status. In this case, the client (user agent) is informed that the resource available at "/squire" has moved temporarily to "".

Remember that no matter what configuration file they are located in, Redirect statements must specify the full path of the redirected resource following the domain name. These statements must also include the full URL of the resource's new location..

To specify a particular HTTP redirection status, specify one of the following status:

File excerpt:Apache configuration option

Redirect permanent /squire
Redirect temp /squire
Redirect seeother /squire
Redirect gone /squire

This redirection tells the client that the resource has moved permanently, which corresponds to HTTP status 301. The "temp" status is the default behavior, specifying that the redirection is only temporary; this corresponds to HTTP status 302. The "seeother" status sends a signal (HTTP status 303) that says the requested resource has been replaced by another resource. Finally, the "gone" status tells the client that the resource has been removed (permanently); this sends the HTTP status 410, as an alternative to the unavailable "404" status. In the case of the "gone" redirection, omit the final URL.

You can also specify specific HTTP codes, as follows.

File excerpt:Apache configuration option

Redirect 301 /squire
Redirect 302 /squire
Redirect 303 /squire
Redirect 410 /squire

Apache also provides two additional directives for permanent and temporary redirections that are a bit more clear. They are as follows:

File excerpt:Apache configuration option

RedirectPermanent /squire/bio.html
RedirectTemp /squire/bio.html

Additionally, Apache makes it possible to redirect a given class of requests to match a given regular expression using the RedirectMatch directive. For example:

File excerpt:Apache configuration option

RedirectMatch (.*)\.jpg$$1.jpg

This directive matches against any request for a file with a .jpg extension and replaces it with a location on a second domain. Therefore:

Beyond URL Redirection

The Redirect directive provides basic functionality to point requests for specific resource to different URLs and can help administrators move content to different servers and locations without breaking existing links. However, many Apache users use the facility to "rewrite" URLs in Apache's mod_rewrite module. If you're struggling to keep your Apache configuration organized or need more control than these Redirect statements can provide, we encourage you to investigate mod_rewrite.

The Linode Library contains an introduction to rewriting URLs with mod_rewrite and Apache, which you might find useful.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Creative Commons License

This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.

Last edited by Amanda Folson on Monday, August 22nd, 2011 (r2412).