What does HTTP/1.1 302 mean exactly?
Some article I read once said that it means jumping (from one URI to another), but I detected this "302" even when there was actually no jumping at all!
Solution 1:
A 302 redirect means that the page was temporarily moved, while a 301 means that it was permanently moved.
301s are good for SEO value, while 302s aren't because 301s instruct clients to forget the value of the original URL, while the 302 keeps the value of the original and can thus potentially reduce the value by creating two, logically-distinct URLs that each produce the same content (search engines view them as distinct duplicates rather than a single resource with two names).
Solution 2:
This question was asked a long ago, while the RFC 2616 was still hanging around. Some answers to this question are based in such document, which is no longer relevant nowadays. Quoting Mark Nottingham who, at the time of writing, co-chairs the IETF HTTP and QUIC Working Groups:
Don’t use RFC2616. Delete it from your hard drives, bookmarks, and burn (or responsibly recycle) any copies that are printed out.
The old RFC 2616 has been supplanted by the following documents that, together, define the HTTP/1.1 protocol:
- RFC 7230: Message Syntax and Routing
- RFC 7231: Semantics and Content
- RFC 7232: Conditional Requests
- RFC 7233: Range Requests
- RFC 7234: Caching
- RFC 7235: Authentication
So I aim to provide an answer based in the RFC 7231 which is the current reference for HTTP/1.1 status codes.
The 302
status code
A response with 302
is a common way of performing URL redirection. Along with the 302
status code, the response should include a Location
header with a different URI. Such header will be parsed by the user agent and then perform the redirection:
Web browsers may change from POST
to GET
in the subsequent request. If this behavior is undesired, the 307
(Temporary Redirect) status code can be used instead.
This is how the 302
status code is defined in the RFC 7231:
6.4.3. 302 Found
The
302
(Found) status code indicates that the target resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client ought to continue to use the effective request URI for future requests.The server SHOULD generate a
Location
header field in the response containing a URI reference for the different URI. The user agent MAY use theLocation
field value for automatic redirection. The server's response payload usually contains a short hypertext note with a hyperlink to the different URI(s).Note: For historical reasons, a user agent MAY change the request method from
POST
toGET
for the subsequent request. If this behavior is undesired, the307
(Temporary Redirect) status code can be used instead.
According to MDN web docs from Mozilla, a typical use case for 302
is:
The Web page is temporarily not available for reasons that have not been unforeseen. That way, search engines don't update their links.
Other status codes for redirection
The RFC 7231 defines the following status codes for redirection:
-
301
(Moved Permanently) -
302
(Found) -
307
(Temporary Redirect)
The RFC 7238 was created to define another status code for redirection:
-
308
(Permanent Redirect)
Refer to this answer for further details.
Solution 3:
A simple way of looking at HTTP 301 vs. 302 redirects is:
Suppose you have a bookmark to "http://sample.com/sample". You use a browser to go there.
A 302 redirect to a different URL at this point would mean that you should keep your bookmark to "http://sample.com/sample". This is because the destination URL may change in the future.
A 301 redirect to a different URL would mean that your bookmark should change to point to the new URL as it is a permanent redirect.
Solution 4:
From Wikipedia:
The HTTP response status code 302 Found is the most common way of performing a redirection. It is an example of industrial practice contradicting the standard.