简体   繁体   English

HTTP重定向代码之间的区别

[英]Difference between HTTP redirect codes

The differences between the various HTTP 3XX redirect codes are not clear to me. 各种HTTP 3XX重定向代码之间的差异对我来说并不清楚。 Yes, I've read the spec, but there seems to be some discrepancy between the standard and actual practice here. 是的,我已经阅读了规范,但这里的标准和实际做法之间似乎存在一些差异。

The 301 redirect code seems clear enough: This means the resource was permanently moved to another URI, and future requests should use that URI. 301重定向代码似乎足够清楚:这意味着资源被永久移动到另一个URI,未来的请求应该使用该URI。

And the 307 redirect code also seems clear: it means the redirect is temporary, and future requests should still use the original URI. 307重定向代码似乎也很清楚:它意味着重定向是临时的,未来的请求仍应使用原始URI。

But I can't tell what the difference is between 302 and 303 , or why either of them are really different from 301 . 但是我不知道302303之间有什么区别,或者为什么它们中的任何一个与301真的不同。 It seems that 302 was originally intended to be a temporary redirect, (like 307 ), but in practice, most browsers treated it like a 303 . 似乎302原本打算成为临时重定向(如307 ),但在实践中,大多数浏览器都将其视为303 But what's the difference between a 303 and a 301 ? 但是303301之间的区别是什么? Is 301 supposed to mean the redirect is more permanent? 301应该意味着重定向永久吗?

  • 301 : Permanent redirect. 301 :永久重定向。 Clients making subsequent requests for this resource should use the new URI. 对此资源发出后续请求的客户端应使用新URI。 Clients should not follow the redirect automatically for POST/PUT/DELETE requests. 客户端不应自动执行重定向的POST / PUT / DELETE请求。
  • 302 : Redirect for undefined reason. 302 :重定向未定义的原因。 Clients making subsequent requests for this resource should not use the new URI. 客户端使此资源应该使用新的URI的后续请求。 Clients should not follow the redirect automatically for POST/PUT/DELETE requests. 客户端不应自动执行重定向的POST / PUT / DELETE请求。
  • 303 : Redirect for undefined reason. 303 :重定向未定义的原因。 Typically, 'Operation has completed, continue elsewhere.' 通常,'行动已经完成,继续在其他地方。 Clients making subsequent requests for this resource should not use the new URI. 客户端使此资源应该使用新的URI的后续请求。 Clients should follow the redirect for POST/PUT/DELETE requests, but use GET for the follow-up request . 客户端遵循POST / PUT / DELETE请求的重定向,但使用GET作为后续请求
  • 307 : Temporary redirect. 307 :临时重定向。 Resource may return to this location at a later point. 资源可能稍后返回此位置。 Clients making subsequent requests for this resource should use the old URI. 对此资源发出后续请求的客户端应使用旧URI。 Clients should not follow the redirect automatically for POST/PUT/DELETE requests. 客户端不应自动执行重定向的POST / PUT / DELETE请求。

I personally recommend avoiding 302 if you have the choice. 如果您有选择,我个人建议避免使用302。 Many clients do not follow the spec when they encounter a 302. For temporary redirects, you should use either 303 or 307, depending on what type of behavior you want on non-GET requests. 许多客户在遇到302时都不遵守规范。对于临时重定向,您应该使用303或307,具体取决于您对非GET请求的行为类型。 Prefer 307 to 303 unless you need the alternate behavior on POST/PUT/DELETE. 除非您需要POST / PUT / DELETE上的备用行为,否则首选307到303。

The difference between 303 and 307 is this: 303和307之间的区别是:

303 : See other. 303 :见其他。 The request is received correctly, but the results should be retrieved using a GET on the redirect url. 请求已正确接收,但应使用重定向网址上的GET检索结果。

307 : Temporary redirect. 307 :临时重定向。 The entire request should be redirected to the new url. 应将整个请求重定向到新网址。 Any post data should be re-posted. 任何帖子数据都应该重新发布。

Note that 302 was intended to have the behavior of 307, but most browsers implemented it as the behavior of 303 (both of which didn't exist back then). 请注意, 302的行为是307,但大多数浏览器都将其实现为303的行为(当时两者都不存在)。 Therefore, those two new codes were introduced to replace 302. 因此,引入了这两个新代码来代替302。

The difference between 301 and 303: 301和303之间的区别:

301 : The document is moved. 301 :移动文档。 Future requests should use the new url. 未来的请求应该使用新的URL。 This url is obsolete. 这个网址已经过时了。

Note: Be careful with this code. 注意:请注意此代码。 Browsers and proxies tend to apply really agressive caching on it, so if you reply with a 301 it might take a long while for someone to revisit that url. 浏览器和代理往往会在其上应用非常激进的缓存,因此如果您使用301回复,则可能需要很长时间才能重新访问该网址。

303 : The request is received correctly. 303 :正确接收请求。 Any PUT requests are processed. 处理任何PUT请求。 The resulting document can be retrieved from the redirect url. 可以从重定向URL检索生成的文档 Future request should still go to the original url. 未来的请求仍应转到原始网址。

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

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