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

1

5

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.

Geesey answered 20/5, 2016 at 3:21 Comment(0)
L
7

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.

Longe answered 28/8, 2016 at 16:28 Comment(1)
Thank you. After some research, including the standard you quoted, I proceeded with the 303 redirect.Geesey

© 2022 - 2024 — McMap. All rights reserved.