gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:20 25 4
gpt4 key购买 nike

我正在用 php 创建一个简单的问答留言板。当有人提交问题的答案时,php 脚本会将用户重定向到原始问题,并在页面底部显示更新的答案。

在这种情况下,使用 301、302 或 303 重定向会更好吗?我在考虑 302 重定向,因为重定向不是永久的或静态的——重定向取决于发送到提交脚本的参数。但是,我也看到一些表单推荐使用 303。

最佳答案

对于这种情况,正确的重定向是 303 重定向,因为您是从表单提交(大概是 POST 请求)重定向到查看页面(应该是 GET 请求),这是一个临时重定向(即重定向可能会根据参数而改变)。

这是由标准支持的:

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.

关于php - 提交表单后我应该使用 301、302 还是 303 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com