随机机会的无关重定向使用什么HTTP响应状态代码? [英] What HTTP response status code to use for unrelated redirects of random chance?

查看:122
本文介绍了随机机会的无关重定向使用什么HTTP响应状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My website has a 1% random chance of redirecting visitors to this YouTube video.

服务器当前发送的是旧版本302,但我希望它具有更好的语义.

The server currently sends the good-old 302, but I want it to have better semantics.

我不知道重定向是永久的还是临时的.从某种意义上说,这是永久性的,因为人们总是有1%的机会被重定向;从某种意义上来说,这是暂时的,因为人们不会每次都被重定向.

I don't know if the redirect is permanent or temporary. It's permanent in the sense that people always have a 1% chance of being redirected, and it's temporary in the sense that people don't get redirected every single time.

当前的3xx响应状态代码均不符合我网站的行为.

None of the current 3xx response status codes match my website's behavior.

301永久移动
此请求和所有以后的请求都应定向到给定的URI.[21]

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

此请求以及所有将来的请求"-不,不是所有将来的请求.

"This and all future requests" - nope, not all future requests.

找到了302(以前是临时​​移动")
告诉客户查看(浏览)另一个URL. 302已被303和307取代.这是违反该标准的行业惯例的示例. HTTP/1.0规范(RFC 1945)要求客户端执行临时重定向(原始描述短语为临时移动"),[22]但是流行的浏览器使用303的功能实现了302.因此,HTTP/1.1添加了状态码303和307来区分这两种行为.[23]但是,某些Web应用程序和框架使用302状态代码就像使用303.[24]

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取代",所以我想这已经过时了但仍很常用?

This is confusing. It does state, "302 has been superseded by 303 and 307," so I guess this is obsolete yet still commonly used?

303另请参见(自HTTP/1.1起)
可以使用GET方法在另一个URI下找到对请求的响应.当收到响应POST(或PUT/DELETE)的响应时,客户端应假定服务器已接收到数据,并应向给定URI发出新的GET请求.[25]

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]

可以在另一个URI下找到对请求的响应"-Rickroll不是对原始请求的响应.它与我的网站无关! 当收到响应POST(或PUT/DELETE)的响应时,客户端应假定服务器已接收到数据,并应向给定URI发出新的GET请求." 303说明的最后一句话不适用于我的重定向.

"The response to the request can be found under another URI" - Rickroll is not the response to the original request. It has nothing to do with my site! "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." The last sentence of the 303 description doesn't work with my redirect.

307临时重定向(自HTTP/1.1起)
在这种情况下,应使用另一个URI重复该请求;但是,将来的请求仍应使用原始URI.与历史上实现302的方式相反,在重新发出原始请求时不允许更改请求方法.例如,应该使用另一个POST请求重复一个POST请求.[30]

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]

在这种情况下,应使用另一个URI重复该请求;但是,以后的请求仍应使用原始URI."是的!这就是我的重定向. "...重新发出原始请求时,不允许更改请求方法.例如,应使用另一个POST请求重复POST请求.不幸的是,这不适用于我的重定向.即使用户提交表单(POST方法),我的网站也会随机对用户进行Rickroll.

"In this case, the request should be repeated with another URI; however, future requests should still use the original URI." Yes! This is what my redirect is. "...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." Unfortunately, this doesn't work with my redirect. My site randomly Rickrolls users even when they submit forms (POST method).

308永久重定向(RFC 7538)
该请求和所有以后的请求应使用另一个URI重复. 307和308与302和301的行为并行,但不允许更改HTTP方法.因此,例如,向永久重定向的资源提交表单可能会继续顺利进行.[31]

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视频.此外,不允许更改方法.

308 has the same issue as 301 because not all future requests should go to the Rickroll video. Furthermore, the method isn't allowed to change.

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

What HTTP response status code should my server send for random-chance redirects that have nothing to do with my actual site?

推荐答案

HTTP响应状态代码的官方来源是 RFC7231 ,而不是Wikipedia. 您应该仔细阅读 RFC7231第6.4节以了解3XX状态代码.

The official source for HTTP response status codes is RFC7231, not Wikipedia. You should peruse RFC7231 Section 6.4 for the explanation of 3XX status codes.

通过随机机会重定向访问者"与实际的重定向无关, 因此,符合您要求的状态代码为303, 在第6.4.4节中对此进行了说明(重点是我的):

The "redirecting visitors by random chance" thing is irrelevant to the actual redirection, therefore the status code that fits your requirement is 303, which is explained in Section 6.4.4 as follows (emphasis mine):

303(请参阅其他)状态代码表示服务器处于 将用户代理重定向到其他资源,如 位置标头字段中的URI,旨在提供一个 间接响应原始请求.用户代理可以执行 针对该URI的检索请求(如果是GET或HEAD请求, 使用HTTP),也可以将其重定向,并显示最终 结果作为对原始请求的答复.请注意,新的URI 位置标头字段中的不等同于 有效的请求URI.

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符合您的要求的三个原因:

303 fits your requirement for three reasons:

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

为什么不是302? 由于第6.4.3节中的这一部分解释(重点是我的):

Why not 302? Because of this part of the explanation in Section 6.4.3 (emphasis mine):

由于历史原因,用户代理可以更改请求 方法(从POST到GET)以进行后续请求.

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

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

In other words, the user agent can use either the same request method or a different request method. The flexibility that the specification allows for 302 doesn't fit your requirement.

这篇关于随机机会的无关重定向使用什么HTTP响应状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆