Search code examples
phpformshttp-redirecthttp-status-code-301http-status-code-302

Should I use a 301, 302, or 303 redirect after form submission?


I am creating a simple question-answer message board in php. When someone submits the answer to a question, the php script redirects the user to the original question, with the updated answer at the bottom of the page.

In this case, would it be better to use a 301, 302, or 303 redirect? I was thinking a 302 redirect because the redirect is not permanent or static – the redirect depends on parameters that are sent to the submission script. However, I have seen 303 recommended for some forms as well.


Solution

  • The correct redirect for this situation would be a 303 redirect, since you are redirecting from a form submission (presumably this would be a POST request) to a viewing page (which should be a GET request) and it is a temporary redirect (i.e. the redirect may change depending on parameters).

    This is backed up by the standard:

    The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.