Skip to content

Fundamentals

Paramesh Gunasekaran edited this page Aug 17, 2021 · 1 revision

URL redirection, also known as URL forwarding, is a technique to give more than one URL address to a page, a form, or a whole Web site/application. HTTP has a special kind of response, called a HTTP redirect, for this operation.

Redirects accomplish numerous goals:

  • Temporary redirects during site maintenance or downtime
  • Permanent redirects to preserve existing links/bookmarks after changing the site's URLs, progress pages when uploading a file, etc.

Principle

In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3, and a Location header holding the URL to redirect to.

When browsers receive a redirect, they immediately load the new URL provided in the Location header. Besides the small performance hit of an additional round-trip, users rarely notice the redirection.

There are several types of redirects, sorted into three categories:

  • Permanent redirections
  • Temporary redirections
  • Special redirections

Permanent redirections

These redirections are meant to last forever. They imply that the original URL should no longer be used, and replaced with the new one. Search engine robots, RSS readers, and other crawlers will update the original URL for the resource.

Code Text Method handling Typical use case
301 Moved Permanently GET methods unchanged Others may or may not be changed to GET. Reorganization of a Web site.
308 Permanent Redirect Method and body not changed Reorganization of a Web site, with non-GET links/operations.

The specification did not intend to allow method changes, but there are existing user agents that do change their method. 308 was created to remove the ambiguity of the behavior when using non-GET methods.

Temporary redirections

Sometimes the requested resource can't be accessed from its canonical location, but it can be accessed from another place. In this case, a temporary redirect can be used.

Search engine robots and other crawlers don't memorize the new, temporary URL. Temporary redirections are also used when creating, updating, or deleting resources, to show temporary progress pages.

Code Text Method handling Typical use case
302 Found GET methods unchanged Others may or may not be changed to GET The Web page is temporarily unavailable for unforeseen reasons.
303 See Other GET methods unchanged Others changed to GET (body lost) Used to redirect after a PUT or a POST, so that refreshing the result page doesn't re-trigger the operation.
307 Temporary Redirect Method and body not changed The Web page is temporarily unavailable for unforeseen reasons. Better than 302 when non-GET operations are available on the site.

The specification did not intend to allow method changes, but there are existing user agents that do change their method. 307 was created to remove the ambiguity of the behavior when using non-GET methods.

Special redirections

304 (Not Modified) redirects a page to the locally cached copy (that was stale), and 300 (Multiple Choice) is a manual redirection: the body, presented by the browser as a Web page, lists the possible redirections and the user clicks on one to select it.

Code Text Typical use case
300 Multiple Choice Not many: the choices are listed in an HTML page in the body. Machine-readable choices are encouraged to be sent as Link headers with rel=alternate.
304 Not Modified Sent for revalidated conditional requests. Indicates that the cached response is still fresh and can be used.
Clone this wiki locally