HTTP Status 301 (Moved Permanently)

HTTP Status 301 indicates that the requested resource has been permanently moved to a new location specified by the ‘Location’ header.

301-Moved-Permanently

HTTP Status 301 is one of the redirection-related statuses, which indicates that the resource requested has been permanently moved to the URI given by the ‘Location‘ header and all future requests should use the new URI.

Redirection is the process of forwarding the request from one URL to a different URL. The specification for Status 301 requires the request method (and the request body) not to be altered when the redirection is performed.

It is recommended to use HTTP 301 only for ‘GET‘ or ‘HEAD‘ methods.

1. Why a resource needs to move permanently?

Generally, changing the URLs of resources is not advisable. Still, we can encounter unavoidable situations where we must make changes to the URL of a resource.

A few such examples can be:

  • Moving the resource from HTTP to HTTPs protocol
  • The resource has been discontinued and an alternate resource is available in the new URL

2. The ‘Location‘ Header

The server SHOULD generate a ‘Location‘ header field in the response containing the new location of the resource.

2.1. Client request

GET /index.php HTTP/1.1
Host: www.example.com

2.2. Server response

HTTP/1.1 301 Moved Permanently
Location: https://example.com/index.asp

3. Response is Cachable

A 301 response is cacheable by default; i.e., unless otherwise indicated by the method definition or explicit cache controls.

Refer to these cache headers for more information.

4. Handling a 301 Response

  • If a client has link-editing capabilities, it should update all references to the requested URL with the new URL.
  • Search engines (Google and Bing) replace the old URL in the search results, and the old URL will eventually disappear. Link juice will pass from the old URL to the new URL.
  • The browsers will automatically detect the 301 response code after that it will read the new location URL and redirect the request to that new location.

Happy Learning !!

Reference: RFC 7231

Leave a Comment

  1. The browsers will automatically detect the most of the 3xx code class which makes it not very suitable for API in cause your clients are java script based Rich clients. This response will not reach the JavaScript code.

    So at least you should only use it how it meant in HTTP. Clear redirection on GET or HEAD and no custom semantics.

    Reply

Leave a Comment