当 Web 服务器希望重定向用户的浏览器时,它应该在响应 header 中放置哪个状态代码(即“200 OK”)?根据我的阅读,答案似乎是 3XX 代码中的任何一个,但这些代码中的每一个似乎都有不同的描述。只要“位置”在响应 header 中,使用哪个是否重要?
最佳答案
这完全取决于您进行重定向的原因。我假设你已经阅读了 RFC 2616 .
您不想使用 301,除非可能用于页面重命名之类的事情。我不知道有任何 CMS 会自动执行此操作。
Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible.
302 非常适合临时 GET-after-GET,并且默认情况下是不可缓存的。它不应该用于 GET-after-POST,因为它实际上意味着 POST-after-POST(在询问用户确认之后):
Note: RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method. The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client.
303 用于 GET-after-POST。旧式浏览器可能不支持它,因此您可能不想将它用于 GET-after-GET:
Note: Many pre-HTTP/1.1 user agents do not understand the 303 status. When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303.
307 用于 POST-after-POST(与用户确认后)。它可以用于 GET-after-GET,但在那种情况下,您还不如使用 302/303:
If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
至于兼容性,如果很大比例(1%?)的用户支持不理解 303 或 307 的损坏代理,即使他们声称支持 HTTP/1.1,我也不会感到惊讶。嗯。
关于http - 哪个是用于重定向的正确 HTTP 响应代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554508/