HTTP Status 301 is one of the redirection related statuses, which indicates that the resource requested has been permanently moved to the URL given by the
Locationheader. And all the 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 URL needs to move permanently?
Generally, changing the URLs of resources is not advisable. Still, we can come across unavoidable situations where we must be making the changes to the URL of a resource.
A few such examples can be:
- Moving the resource from HTTP to HTTPs protocol
- Resource has been discountinued and alternate resource is available in new URL
2. 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
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. Response Handling
- 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.
Reference : RFC 7231