What's the difference between HTTP 301 and 308 status codes?
An overview of 301
, 302
and 307
The RFC 7231, the current reference for semantics and content of the HTTP/1.1 protocol, defines the 301
(Moved Permanently) and 302
(Found) status code, that allows the request method to be changed from POST
to GET
. This specification also defines the 307
(Temporary Redirect) status code that doesn't allow the request method to be changed from POST
to GET
.
See more details below:
The
301
(Moved Permanently) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. [...]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.
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. [...]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.
The
307
(Temporary Redirect) status code indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. Since the redirection can change over time, the client ought to continue using the original effective request URI for future requests. [...]Note: This status code is similar to
302
(Found), except that it does not allow changing the request method fromPOST
toGET
. This specification defines no equivalent counterpart for301
(Moved Permanently) (RFC 7238, however, defines the status code308
(Permanent Redirect) for this purpose).
The need for 308
The RFC 7238 has been created to define the 308
(Permanent Redirect) status code, that is similar to 301
(Moved Permanently) but does not allows the request method to be changed from POST
to GET
.
The 308
status code is now defined by the RFC 7538 (that obsoleted the RFC 7238).
The
308
(Permanent Redirect) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. Clients with link editing capabilities ought to automatically re-link references to the effective request URI to one or more of the new references sent by the server, where possible. [...]Note: This status code is similar to
301
(Moved Permanently), except that it does not allow changing the request method fromPOST
toGET
.
Se we have the following:
+-----------+-----------+
| Permanent | Temporary |
+------------------------------------------------------------+-----------+-----------+
| Allows changing the request method from POST to GET | 301 | 302 |
+------------------------------------------------------------+-----------+-----------+
| Doesn't allow changing the request method from POST to GET | 308 | 307 |
+------------------------------------------------------------+-----------+-----------+
Choosing the most suitable status code
Michael Kropat put together a set of decision charts that helps to determine the best status code for each situation. See the following for 2xx
and 3xx
status codes:
Related videos on Youtube
Alexander Drobyshevsky
Updated on July 08, 2022Comments
-
Alexander Drobyshevsky almost 2 years
What's the difference between HTTP
301
and308
status codes?301
(Moved Permanently): This and all future requests should be directed to the given URI.308
(Permanent Redirect): The request and all future requests should be repeated using another URI.
They seem to be similar.
-
KonstantinL over 7 yearsNo code 308 in tools.ietf.org/html/rfc2616 and tools.ietf.org/html/rfc6585, so the question has to be addressed to the inventor of this non-standard code.
-
Alexander Drobyshevsky over 7 yearsThere is specification RFC 7538, therefore it is real HTTP code
-
KonstantinL over 7 yearsWell, tools.ietf.org/html/rfc7538: Note: This status code is similar to 301, except that it does not allow changing the request method from POST to GET.
-
cassiomolin over 7 yearsDon't use the RFC 2616 as reference. It was obsoleted by the RFCs 7230-35.
-
R. Schreurs almost 6 yearsGiven that the question was specifically about the destinction between 301 and 308, could you give some more explanation on: "does not allows the request method to be changed from
POST
toGET
"? Would it mean dat a posted form cannot be processed, but a fresh new form could be serverd and then be posted on a next request? -
Bruce Adams over 5 yearsThis draft specification (tools.ietf.org/id/draft-hunt-http-rest-redirect-00.html) suggests that ReSTful services should use 308 even for GETs. "HTTP redirection codes 301-306 SHOULD NOT be used unless the service provider is aware the client is in fact a user-agent." However this is only a draft. I'm not sure if/when it will be accepted.
-
SherylHohman about 4 yearsThis post, the-definitive-guide-to-get-vs-post, clarifies why allowing a
POST
(safe) request to be changed into a toGET
(unsafe in that data is passed by adding it to the url - and urls can be saved - including passwords) request can be a security issue, and generally should be avoided, unless you know it to be safe to change. These days it seems that it's generally supported and preferred to use 307, 308 over 301, 302. But you should verify. -
SherylHohman about 4 yearsMnemonic
308
is like a sideways infinity, so Permanent redirect, and also never change the request Method - it is also a permanent, fixed request type. Then ,307
is1
step below - permanent/keep request Method (Get/Post), but redirect to a temp location: 7 - looks like "left turn" or temp detour, and 7 is also similar looking to k, so "keep" the request method. -
Knu almost 4 yearsYou forgot to mention Resume Incomplete.
-
cassiomolin almost 4 years@Knu That's not a standard status code for HTTP, at least not with that meaning:
308
has been standardised as Permanent Redirect.