DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.8 Redirecting to Another Location

Problem

You want requests to a particular URL to be redirected to another server.

Solution

Use a Redirect directive in httpd.conf, and give an absolute URL on the second argument:

Redirect /example http://www.other.server/new/location

Discussion

Whereas Alias maps a URL to something in the local filesystem, Redirect maps a URL to another URL, usually on another server. The second argument is a full URL and is sent back to the client (browser), which makes a second request for the new URL.

It is also important to know that the Redirect directive preserves path information, if there is any. Therefore, this recipe redirects a request for http://original.server/example/something.html to http://www.other.server/new/location/something.html.

Redirections come in several different flavors, too; you can specify which particular type of redirect you want to use by inserting the appropriate keyword between the Redirect directive and the first URL argument. All redirects instruct the client where the requested document is now; the different types of redirection inform where the client should look for the document in the future.


temp

A temporary redirection is used when the document is not in the originally requested location at the moment, but is expected to be there again some time in the future. So the client remembers the URL it used on the original request and will use it on future requests for the same document.


permanent

A permanent redirection indicates that not only is the requested document not in the location specified by the client, but that the client should never look for it there again. In other words, the client should remember the new location indicated in the redirect response and look there in all subsequent requests for the resource.


gone

This keyword means that the document doesn't exist in this location, and it shouldn't bother asking any more. This differs from the 404 Not Found error response in that the gone redirection admits that the document was once here, even though it isn't any more. It's not considered an error, unlike the 404 status.


seeother

A seeother redirection tells the client that the original document isn't located here any more and has been superseded by another one in a different location. That is, the original request might have been for:

http://example.com/chapter2.html

but the server answers with a seeother redirection to:

http://bookname.com/edition-2/chapter2.html

indicating not only a new location, but that the original Chapter 2 has been superseded by the content of the second edition.

By default, if no keyword is present, a temporary redirection is issued.

Here's an example of the various directive formats, including the HTTP status code number in case you want to use an ErrorDocument to customize the server's response text:

#
# These are equivalent, and return a response with a 302 status.
#
Redirect      /foo.html http://example.com/under-construction/foo.html
Redirect temp /foo.html http://example.com/under-construction/foo.html
RedirectTemp  /foo.html http://example.com/under-construction/foo.html
#
# These are equivalent to each other as well, returning a 301 status
#
Redirect permanent /foo.html http://example.com/relocated/foo.html
RedirectPermanent  /foo.html http://example.com/relocated/foo.html
#
# This tells the client that the old URL is dead, but the document
# content has been replaced by the specified new document.  It
# returns a 303 status.
#
Redirect seeother /foo.html http://example.com/relocated/bar.html
#
# Returns a 410 status, telling the client that the document has been
# intentionally removed and won't be coming back.  Note that there
# is no absoluteURL argument.
#
Redirect gone     /foo.html

See Also

    [ Team LiB ] Previous Section Next Section