什么 HTTP 响应状态代码用于随机机会的无关重定向?

What HTTP response status code to use for unrelated redirects of random chance?

我的网站有 1% 的随机机会将访问者重定向到 this YouTube video.

服务器当前发送的是老式的 302,但我希望它具有更好的语义。

我不知道重定向是永久的还是临时的。它是永久性的,因为人们总是有 1% 的机会被重定向,它是暂时的,因为人们不会每次都被重定向。

None 个当前 3xx 响应状态代码与我网站的行为匹配。

HTTP 3xx 重定向(来源:维基百科)

301 Moved Permanently
This and all future requests should be directed to the given URI.[21]

"This and all future requests" - 不,不是所有未来的请求。

302 Found (Previously "Moved temporarily")
Tells the client to look at (browse to) another url. 302 has been superseded by 303 and 307. This is an example of industry practice contradicting the standard. The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"),[22] but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours.[23] However, some Web applications and frameworks use the 302 status code as if it were the 303.[24]

这令人困惑。它确实声明,“302 已被 303 和 307 取代”,所以我想这是过时但仍然常用的?

303 See Other (since HTTP/1.1)
The response to the request can be found under another URI using the GET method. When received in response to a POST (or PUT/DELETE), the client should presume that the server has received the data and should issue a new GET request to the given URI.[25]

"The response to the request can be found under another URI" - Rickroll 不是对原始请求的响应。这与我的网站无关! "When received in response to a POST (or PUT/DELETE), the client should presume that the server has received the data and should issue a new GET request to the given URI." 303 描述的最后一句话不适用于我的重定向。

307 Temporary Redirect (since HTTP/1.1)
In this case, the request should be repeated with another URI; however, future requests should still use the original URI. In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the original request. For example, a POST request should be repeated using another POST request.[30]

"In this case, the request should be repeated with another URI; however, future requests should still use the original URI." 是的!这就是我的重定向。 “...重新发出原始请求时,不允许更改请求方法。例如,应使用另一个 POST 请求重复 POST 请求。”不幸的是,这不适用于我的重定向。即使用户提交表单(POST 方法),我的网站也会随机 Rickrolls 用户。

308 Permanent Redirect (RFC 7538)
The request and all future requests should be repeated using another URI. 307 and 308 parallel the behaviors of 302 and 301, but do not allow the HTTP method to change. So, for example, submitting a form to a permanently redirected resource may continue smoothly.[31]

308 与 301 有相同的问题,因为并非所有未来的请求都应该转到 Rickroll 视频。此外,该方法不允许更改。

对于与我的实际站点无关的随机重定向,我的服务器应该发送什么 HTTP 响应状态代码?

HTTP 响应状态代码的官方来源是 RFC7231,而非维基百科。 您应该仔细阅读 RFC7231 Section 6.4 以了解 3XX 状态代码的解释。

“随机重定向访问者”与实际重定向无关, 因此符合您要求的状态代码是 303, 在 Section 6.4.4 中解释如下(强调我的):

The 303 (See Other) status code indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, which is intended to provide an indirect response to the original request. A user agent can perform a retrieval request targeting that URI (a GET or HEAD request if using HTTP), which might also be redirected, and present the eventual result as an answer to the original request. Note that the new URI in the Location header field is not considered equivalent to the effective request URI.

303 符合您的要求,原因有以下三个:

  1. “到不同的资源”:您的访问者被重定向到的资源与他们最初请求的资源不同。
  2. “执行检索请求”:使用 GET 或 HEAD 请求获取最终资源。
  3. “不认为是等价的”:原来的请求URI不能被新的URI代替,因为它不等价,换句话说,是临时的。

为什么不是 302? 由于Section 6.4.3中的这部分解释(强调我的):

For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request.

换句话说,用户代理可以使用相同的请求方法,也可以使用不同的请求方法。 规范允许的 302 灵活性不符合您的要求。