这些HTTP 3xx状态代码有什么区别?

时间:2014-08-26 20:35:19

标签: http redirect

我正在阅读the specification about HTTP/1.1重定向代码。我有很多问题:

  1. 有这样的文字:

    If the 301/302/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.
    

    :浏览器应用于什么意义?我从未看到任何301/302响应,使我的浏览器询问我是否要确认操作(例如:通过PHP接收表单然后重定向到新输出)。

  2. 我很清楚,大部分时间我们使用302而不是303来处理和重定向(参见其他 - 这是为了这个目的而创建的)。但是,我对301 302(请注意:预期的原始行为)和307感到困惑。 Q :在实际示例中它们之间有什么区别/用例?

1 个答案:

答案 0 :(得分:1)

大多数301/302重定向响应发生在常规GET请求上(与POST请求相反),因此无需确认。此外,即使RFC在技术上建议确认非GET / HEAD请求,RFC也提到:

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.

302告诉用户代理在将来的请求中继续使用旧的uri。 303告诉用户代理在将来的请求中使用新的uri。

此外,正如RFC中的说明链接到提及:

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.

我认为应该更改RFC以便If the 301/302 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.

应改写为If the 301/302 status code is received in response to a request other than GET or HEAD, the user agent SHOULD 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.

由于大多数当前用户代理都违反了RFC。