302 FOUND和307 TEMPORARY REDIRECT HTTP响应之间的区别是什么?

W3规范似乎表明它们都用于临时重定向,并且都不能缓存,除非响应特别允许。


当前回答

另外,对于服务器管理员,重要的是要注意,如果使用307重定向,浏览器可能会向用户显示提示。

例如,Firefox和Opera会要求用户允许重定向,而Chrome、IE和Safari会透明地进行重定向。

*每个防弹SSL和TLS(第192页)。

其他回答

最初只有302个

Response What browsers should do
302 Found Redo request with new url

这个想法是:

如果你在某个位置做一个GET,你会重做你的GET到新的URL 如果你在某个位置做POST,你会重做POST到新的URL 如果你在某个位置执行PUT,你会重做PUT到新的URL 如果你在某个位置执行DELETE操作,你会对新的URL重做DELETE操作 等

不幸的是,每个浏览器都做错了。当得到302时,他们总是会在新的URL上切换到GET,而不是用相同的动词(例如,POST)重新尝试请求:

Mosaic做错了 Netscape复制了Mosaic中的漏洞;所以他们错了 ie浏览器复制了Netscape的漏洞;所以他们错了

这实际上是错误的。

所有浏览器都错了302。303和307就这样诞生了。

Response What browsers should do What browsers actually do
302 Found Redo request with new url GET with new url
303 See Other GET with new url GET with new url
307 Temporary Redirect Redo request with new url Redo request with new url

图表形式

5种不同的重定向:

╔═══════════╦════════════════════════════════════════════════╗
║           ║                Switch to GET?                  ║
║           ╟────────────────────────┬───────────────────────╢
║ Temporary ║          No            │         Yes           ║
╠═══════════╬════════════════════════╪═══════════════════════╣
║ No        ║ 308 Permanent Redirect │ 301 Moved Permanently ║
╟───────────╟────────────────────────┼───────────────────────╢
║ Yes       ║ 307 Temporary Redirect │ 303 See Other         ║
║           ║ 302 Found (intended)   │ 302 Found (actual)    ║
╚═══════════╩════════════════════════╧═══════════════════════╝

另外:

Response Switch to get? Temporary?
301 Moved Permanently No No
302 Found (intended) No Yes
302 Found (actual) Yes Yes
303 See Other Yes Yes
307 Temporary Redirect No Yes
308 Permanent Redirect No No

区别在于重定向POST, PUT和DELETE请求,以及服务器对用户代理行为的期望(RFC 2616):

注意:RFC 1945和RFC 2068指定不允许客户端这样做 更改重定向上的方法 请求。然而,大多数现有用户 代理实现将302视为 这是303响应,执行一个 GET Location字段值 不管最初的请求是什么 方法。状态代码是303和307 已添加的服务器,希望 明确地说明是哪一种 的反应是预期的 客户端。

另外,请阅读维基百科关于30x重定向代码的文章。

307的出现是因为用户代理采用了一种事实上的行为,即接收到302响应的POST请求并向Location响应头发送GET请求。

这是不正确的行为——只有303才会导致POST转换为GET。如果最初的POST请求返回302,用户代理在请求新URL时应该(但不)坚持使用POST方法。

引入307是为了允许服务器向用户代理清楚地表明,当客户端遵循Location响应报头时,不应该对方法进行更改。

302 is temporary redirect, which is generated by the server whereas 307 is internal redirect response generated by the browser. Internal redirect means that redirect is done automatically by browser internally, basically the browser alters the entered url from http to https in get request by itself before making the request so request for unsecured connection is never made to the internet. Whether browser will alter the url to https or not depends upon the hsts preload list that comes preinstalled with the browser. You can also add any site which support https to the list by entering the domain in the hsts preload list of your own browser which is at chrome://net-internals/#hsts.One more thing website domains can be added by their owners to preload list by filling up the form at https://hstspreload.org/ so that it comes preinstalled in browsers for every user even though I mention you can do particularly for yourself also.

Let me explain with an example: I made a get request to http://www.pentesteracademy.com which supports only https and I don't have that domain in my hsts preload list on my browser as site owner has not registered for it to come with preinstalled hsts preload list. GET request for unsecure version of the site is redirected to secure version(see http header named location for that in response in above image). Now I add the site to my own browser preload list by adding its domain in Add hsts domain form at chrome://net-internals/#hsts, which modifies my personal preload list on my chrome browser.Be sure to select include subdomains for STS option there. Let's see the request and response for the same website now after adding it to hsts preload list. you can see the internal redirect 307 there in response headers, actually this response is generated by your browser not by server. Also HSTS preload list can help prevent users reach the unsecure version of site as 302 redirect are prone to mitm attacks. Hope I somewhat helped you understand more about redirects.

307和302之间的唯一区别是307保证在重定向请求发出时方法和主体不会被更改。对于302,一些旧客户端错误地将方法更改为GET:使用非GET方法和302的行为在Web上是不可预测的,而使用307的行为是可预测的。对于GET请求,它们的行为是相同的。

参考文献:307临时重定向