Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 683
Leo Galleguillos Avatar asked May 20 '16 03:05

Leo Galleguillos


People also ask

When would you use a 303 redirect?

303: Redirect for undefined reason. Typically, 'Operation has completed, continue elsewhere. ' Clients making subsequent requests for this resource should not use the new URI. Clients should follow the redirect for POST/PUT/DELETE requests, but use GET for the follow-up request.

Which is better 301 or 302 redirect?

For permanent changes to a website and continued ranking through SEO, a 301 redirect is necessary. On the other hand, if you're only performing a temporary change, a 302 redirect is better. It tells the search engine that the changes are temporary and may not impact the original page's SEO ranking.

When would you use a 302 redirect?

A 302 redirect is a temporary redirect. This is what you use when you want to redirect a visitor from one page to another for just a short period. For example, if you are redesigning a portion of your site and need to redirect users for a few days to a different page, you might use the 302 redirect.

Which redirect should I use?

301 redirects are permanent, whereas 302 redirects are temporary. A 301 is used when a page has permanently changed location, and a 302 should be used if you intend to move the page back under the original URL in the future. In general, you should expect to use 301 redirects on your website.


1 Answers

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.

like image 194
hifkanotiks Avatar answered Sep 30 '22 04:09

hifkanotiks