简体   繁体   English

表单提交后我应该使用301,302还是303重定向?

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

I am creating a simple question-answer message board in php. 我在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. 当有人提交问题的答案时,php脚本会将用户重定向到原始问题,并在页面底部显示更新后的答案。

In this case, would it be better to use a 301, 302, or 303 redirect? 在这种情况下,使用301,302或303重定向会更好吗? 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. 我在考虑302重定向,因为重定向不是永久性的或静态的 - 重定向取决于发送到提交脚本的参数。 However, I have seen 303 recommended for some forms as well. 但是,我已经看到303推荐用于某些形式。

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 (ie the redirect may change depending on parameters). 这种情况的正确重定向将是303重定向,因为您从表单提交(可能是POST请求)重定向到查看页面(应该是GET请求)并且它是临时重定向(即重定向可能会根据参数而改变)。

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. 可以在不同的URI下找到对请求的响应,并且应该使用该资源上的GET方法检索。 This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. 此方法主要用于允许输出POST激活的脚本以将用户代理重定向到选定的资源。 The new URI is not a substitute reference for the originally requested resource. 新URI不是最初请求的资源的替代引用。 The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable. 303响应绝不能被缓存,但对第二个(重定向)请求的响应可能是可缓存的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM