What is the difference between a 302
and 303
response?
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
- 10.3.3 302 Found
- 10.3.4 303 See Other
Are these interchangeable or why would one be used over the other? Could you please provide a use case of when one would be used (and the other would not) ?
The description on the page to which you linked seem to be fairly descriptive of their intended purpose:
A 302 redirect indicates that the redirect is temporary -- clients should check back at the original URL in future requests.
A 303 redirect is meant to redirect a
POST
request to aGET
resource (otherwise, the client assumes that the request method for the new location is the same as for the original resource).If you're redirecting a client as part of your web application but expect them to always start at the web application (for example, a URL shortener), a 302 redirect seems to make sense. A 303 redirect is for use when you are receiving
POST
data from a client (e.g., a form submission) and you want to redirect them to a new web page to be retrieved usingGET
instead ofPOST
(e.g., a standard page request).But see this note from the status code definitions -- most clients will do the same thing for either a 302 or 303:
There are five different redirect types. Originally there were only two (301 and 302) but most clients implemented them incorrectly so three more were added to clarify the difference between the two different possible behaviours.
The RFC you linked to states this in the section on 302 redirects:
The original redirects
For both of the above, the follow-up request should use the same method (POST, GET, CONNECT, PUT, DELETE, etc.) as the original request and for anything other than GET and HEAD requests, the client should prompt the user before making the request.
Unfortunately this is the part that clients sometimes get wrong and most of them change the method for the follow-up request to GET, regardless of the original method. Because of this, three more redirect codes were created:
The newer redirects
Modern clients should not have any issues with these newer redirects.
Redirect types (301,302,303...) used have a lot of impact on how search engines will index and rank content. Some spiders might even refuse to index temporarily redirected content. Details can be found in various SEO literature...