HTTP Redirections

Redirections with HTTP is used to send client requests to alternate servers, web pages, applications, or forms.

HTTP response codes are used not only to indicate the failure or success of a HTTP request but also, to provide information to the client on how to proceed. One such example is the HTTP 3xx response code category, which indicates an HTTP redirection and is also known as URL forwarding.

URL forwarding

An HTTP redirect is used for a variety of reasons, some of which include accommodating resources that have moved either temporarily or permanently. A temporary redirect might be used during a period of site maintenance, or during an outage, whereas a permanent redirect is used when the URL is changing forever.

HTTP responses that include HTTP redirection status codes also contain the HTTP Location response header to inform the client of the new URL for the resource. When a web browser receives this type of HTTP response, it immediately loads the resource that is indicated by the Location header.

Temporary versus permanent redirections

A temporary redirection is intended to last for a short time, such as to accommodate for an outage or while the main site is undergoing maintenance. Available temporary redirects are 302 Found, 303 See Other and 307 Temporary Redirect.

A permanent redirection is intended to last forever, meaning that the former URL is not to be used again. Available permanent redirects are 301 Moved Permanently and 308 Permanent Redirect.

The distinction between temporary and permanent requests is important, in part, because of how they are indexed by search engines. Specifically, when a redirection is permanent then it is prudent for an search engine to modify the stored URL and reduce crawling of the old URL while refocusing crawling and indexing efforts on the newer URL. However, if a resource is only moved temporarily then leaving it as-is will avoid having to correct it again in the presumably near future while search engines keep crawling both variations.

Note

If you need assistance with SEO and redirects, contact ex-Google SEO consultants Search Brothers.

Available HTTP redirect status codes

Temporary redirections

Note

When a temporary redirect is served for a prolonged period of time, search engines may reassess how to handle the redirect response and change its status to permanently redirected within their own indexes and act accordingly going forward.

302 Found

The HTTP 302 Found redirect message indicates that the requested resource has been temporarily moved and that a second, otherwise identical HTTP request will be made to fetch the resource. The original specification did not intend for clients to change the type of HTTP request method; however, to accommodate situations where this was implemented, this redirection type does not force clients to preserve the HTTP request method.

In situations where the server intends something more specific, it has the option to return HTTP 303 See Other or HTTP 307 Temporary Redirect to remove ambiguity.

303 See Other

The HTTP 303 See Other redirect message indicates that the result of the HTTP request is viewable at an alternate Location, which can be accessed using a GET method. This differs from HTTP 302 Found because the post-redirect fetch request must be HTTP GET, which may or may not be the same HTTP method as the original HTTP request.

An example use case is after a user has filled out a form, a redirection to a textual resource, accessed using HTTP GET, can inform the client that the form has been successfully submitted.

307 Temporary Redirect

The HTTP 307 Temporary Redirect message indicates that the result of the HTTP request is viewable at an alternate Location, and it will be accessed using the same HTTP request method used for the original HTTP request. The 307 Temporary Redirect status code was created because clients were often treating HTTP response 302 Found as if it were HTTP response 303 See Other.

Permanent redirections

301 Moved Permanently

The HTTP 301 Moved Permanently redirect message indicates that the resource has moved to a new URL that is specified within the HTTP response. Clients and caching servers need to update their internal data to reflect the new Location and no longer use the original URL.

Similar to HTTP 302 Found, HTTP response status code 301 Moved Permanently allows clients to change the HTTP request method when fetching the resource post-redirect. For example, if the original HTTP request is an HTTP POST then the client is permitted to use HTTP GET for the post-redirect follow-up HTTP request.

308 Permanent Redirect

The HTTP 308 Permanent Redirect redirect message indicates that the resource has moved to a new URL that is specified within the HTTP response with the Location header. Clients need to update their internal data accordingly and when making the follow-up HTTP request, they are not permitted to change the HTTP request method.

Special redirections

A special redirection occurs for reasons other than the moving of a resource. There are only two special redirections: 300 Multiple Choices and 304 Not Modified.

300 Multiple Choices

HTTP response 300 Multiple Choices is returned by the server to indicate that more than one HTTP response is available as a result of the HTTP request. It indicates success, although it is the client’s responsibility to select which of the presented options best fits their requirements.

304 Not Modified

HTTP response 304 Not Modified is returned by the server to indicate a successful HTTP request, yet the client already has the most recent version of the resource stored in its cache. The HTTP redirection, in this context, is redirected to the client’s internal cache.

Deprecated or not used

305 Use Proxy

The HTTP 305 Use Proxy redirect message indicates that the HTTP request needs to be made through a proxy, and specifies the Location where a subsequent, identical HTTP request shall be made. However this HTTP response has been deprecated and as such, is not to be used.

306 Switch Proxy

The 306 Switch Proxy is a status code that is not currently used and has been reserved for future use. It was originally intended to instruct a client to use an alternative proxy to retrieve the requested resource.

Examples

Apache

Setting up the Apache webserver for redirects can be defined in the .htaccess file as follows:

Temporary redirect

# Redirects the root path (often different hostname) to https://example.re/
Redirect 302 / https://example.re/

Permanent redirect

# Redirects any request for https://example.re/index.html to https://example.re/
Redirect 301 /index.html https://example.re/

Permanent redirect

# Redirects any pattern in https://example.re/old/ to the same files
# in the https://example.re/new/ subdirectory.
RewriteEngine On
RewriteRule ^old/(.*)$ /new/$1 [R=301,NC,L]

PHP

Temporary redirect

<?php
// Temporary redirect to https://example.re/docs
header("HTTP/1.1 302 Found");
header("Location: https://example.re/docs");
exit();
?>

Permanent redirect

<?php
// Permanent redirect to https://example.re/docs
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://example.re/docs");
exit();
?>

Shorter version of permanent redirect

<?php
// Permanent redirect to https://example.re/docs
header("Location: https://example.re/docs", true, 301);
exit();
?>

Non-System Specific

Independent of the server or client that is being used, these more general examples are typical of where HTTP redirections are employed.

Mandatory Secure HTTP

Redirecting HTTP requests from an HTTP version of a page to an HTTPS version will enforce usage of the secure version, as long as the scheme is supported by the client.

Broadening Scope

Simple mapping to help users more easily find a website. For example, the main site is located at:

http://www.example.re

However users may request the hostname without the www-subdomain, also known as the naked-domain name, and the user can then be redirected from the alternate hostname to the main hostname:

http://example.re

Changing Domains

From time to time, such as when a website is given a new name, domain names may change. In these cases, redirecting all of the resources to the new domain name assists users as well as search engines with finding the new domain name.

HTTP redirection alternatives

Although HTTP redirection is the standard approach to redirecting clients to an alternate URL, webmasters do not always have that degree of control over the server. Alternatives include using meta, JavaScript and crypto redirection.

Meta redirections

In the following example, the meta element is used with the http-equiv attribute set to refresh. This will cause the browser to redirect the client to the specified URL. The content attribute specifies the URL and the number of seconds to wait before the redirect happens.

With the content attribute set to zero, the redirect is not delayed and search engines interpret this as a permanent redirect. If the content attribute is set to a nonzero value then the redirect will be delayed and search engines will interpret this as a temporary redirect.

<meta http-equiv="refresh" content="0; [[URL]]=https://example.re/">

JavaScript redirection

In order to effect a redirection using JavaScript, a URL string is to set the window.location property. This forces the specified page to load.

In the following example, the window.location property is to the new URL of the resource.

<!doctype html>
<html>
    <head>
        <script>
            window.location.href("https://example.re")
        </script>
        <title>Welcome to Example.ai</title>
    </head>
    <body>
    ...
    </body>
</html>

Crypto redirection

Crypto redirection is an old descriptive name for what is nowadays is more often referred to as a soft 404, where the URL responds with a 200 OK status code and a link to the new URL with an explanatory message that the content has moved to a new location.

Redirection precedence

In the event that multiple redirects are included, HTTP redirection are given the highest priority. This type of redirect is available even in cases where there is no page data transmitted. The second priority is given to HTML redirects that use the meta element. The third priority is given to JavaScript redirection, but only when JavaScript is enabled and executed. Finally, crypto redirection takes the lowest priority.

Infinite redirection loops

If a server is set up incorrectly, or the client has data corruption in its cookies or cache, it can lead to a situation where circular loops exist. These can sometimes be detected by the server, which may then return the HTTP 500 Internal Server Error status code. However, if there are many servers involved and no single device has enough information to make this determination, some browsers are able to detect this and present the user with an error on the client-side. Clearing cookies and the browser cache may solve the redirection loop if the cause is due to data corruption on the client-side.

Takeaway

When a client attempts to retrieve a resource from a URL where the resource is no longer hosted, either permanently or on a temporary basis, a redirect is used to inform the client of the proper location.

Last updated: August 2, 2023