哪个是正确的重定向 HTTP 响应代码?
当 Web 服务器希望重定向用户的浏览器时,应将哪个状态代码(即“200 OK”)放置在响应标头中?从我的阅读看来,答案可能是 3XX 代码中的任何一个,但每个代码似乎都有不同的描述。只要响应头中包含“Location”,使用哪个还重要吗?
When a web server wishes to redirect a user's browser, which status code (ie, "200 OK") should it place in the response header? From my reading it seems the answer could be any one of the 3XX codes, but each of those codes seems to have a different description. Does it even matter which is used so long as "Location" is in the response header?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这完全取决于您执行重定向的原因。我假设您已阅读 RFC 2616。
您不想使用 301,除非可能用于页面重命名之类的事情。我不知道有任何 CMS 可以自动执行此操作。
302 非常适合临时 GET-after-GET,并且默认情况下是不可缓存的。它不应该用于 GET-after-POST,因为它实际上意味着 POST-after-POST(在要求用户确认之后):
303 代表 GET-after-POST。古老的浏览器可能不支持它,所以你可能不想将它用于 GET-after-GET:
用于 POST-after-POST(与用户确认后)。它可以用于 GET-after-GET,但在这种情况下你最好使用 302/303:
至于兼容性,如果很大比例(1%?)的用户使用不理解 303 或 307 的损坏代理,即使他们声称支持 HTTP/1.1,我也不会感到惊讶。嗯。
It depends entirely on why you're doing the redirect. I'll assume you've read RFC 2616.
You don't want to use 301 except potentially for things like page-renames. I am not aware of any CMS that does this automatically.
302 is perfectly fine for a temporary GET-after-GET and is, by default, uncacheable. It should not be used for a GET-after-POST, since it actually means POST-after-POST (after asking the user for confirmation):
303 is for GET-after-POST. Ancient browsers might not support it, so you might not want to use it for GET-after-GET:
307 is for POST-after-POST (after confirming with the user). It can be used for GET-after-GET, but in that case you might as well use 302/303:
As for compatibility, I wouldn't be surprised if a significant percentage (1%?) of users are behind broken proxies that don't understand 303 or 307, even if they claim to support HTTP/1.1. Meh.
为了节省我大量的打字时间 - 阅读此< /a> 和 这个。
注意 - 并非所有 3xx 代码都会进行重定向。但301、302、303和307的语义是相似的。
To save me a lot of typing - read this and this.
NB - not all the 3xx codes do redirection. But the semantics of 301, 302, 303 and 307 are similar.
根据 Mozilla 文档:
有关状态代码的更多详细信息,请参阅W3C。有关不同状态代码如何影响 SEO 的直观表示,请参阅 HTTP 状态 SEO 指南代码。
According to the Mozilla docs:
More detailed information about status codes can be found from the W3C. For a visual representation of how different status codes affect SEO, see SEO Guide to HTTP Status Codes.
响应代码 302。或者至少,这是当您调用
sendRedirect()
时 Java 的HttpServletResponse
发送的代码。如果 Java 就是这么做的,那么可能是有原因的。301和302在语义上的唯一区别是301表示“永久重定向”而302表示“临时重定向”。这是否会转化为实践中的任何差异完全取决于实施协议的客户端。
例如,浏览器可以决定,由于 301 是永久性的,因此它只会记住它返回的重定向 URL,而不再实际发送对原始 URL 的请求。但这完全取决于浏览器的实现。
也许一个合理的经验法则是,如果您要将静态内容移动到新位置,请始终使用 301。但是,如果您要发送重定向以响应对服务器上某些动态代码的请求,那么您应该使用代码 302 (或者,307)以确保后续请求仍发送到原始 URL,以防您决定更改/更新动态代码以执行不同的操作。
Response code 302. Or at least, that's what Java's
HttpServletResponse
sends when you callsendRedirect()
. And if that's how Java does it, there's probably a reason.The only difference between 301 and 302 semantically is that 301 indicates a "permanent redirect" and 302 indicates a "temporary redirect". Whether this translates into any difference in practice is entirely up to the client implementing the protocol.
For instance, the browser could decide that since 301 is permanent it will just remember the redirect URL that it gets back and never actually send the request for the original URL anymore. But that would be entirely up to the browser implementation.
Perhaps a reasonable rule of thumb is that if you are moving static content to a new location, always use 301. But if you are sending the redirect in response to a request to some dynamic code on your server, then you should use code 302 (or alternately, 307) to ensure that subsequent requests are still sent to the original URL in case you ever decide to change/update your dynamic code to do something different.