哪个是正确的重定向 HTTP 响应代码?

发布于 2024-11-18 07:01:32 字数 127 浏览 1 评论 0原文

当 Web 服务器希望重定向用户的浏览器时,应将哪个状态代码(即“200 OK”)放置在响应标头中?从我的阅读看来,答案可能是 3XX 代码中的任何一个,但每个代码似乎都有不同的描述。只要响应头中包含“Location”,使用哪个还重要吗?

When a web server wishes to redirect a user's browser, which status code (ie, "200 OK") should it place in the response header? From my reading it seems the answer could be any one of the 3XX codes, but each of those codes seems to have a different description. Does it even matter which is used so long as "Location" is in the response header?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

_畞蕅 2024-11-25 07:01:32

这完全取决于您执行重定向的原因。我假设您已阅读 RFC 2616

您不想使用 301,除非可能用于页面重命名之类的事情。我不知道有任何 CMS 可以自动执行此操作。

具有链接编辑功能的客户端应该自动
将对 Request-URI 的引用重新链接到一个或多个新的
如果可能,服务器返回的引用。

302 非常适合临时 GET-after-GET,并且默认情况下是不可缓存的。它不应该用于 GET-after-POST,因为它实际上意味着 POST-after-POST(在要求用户确认之后):

注意:RFC 1945 和 RFC 2068 指定不允许客户端
更改重定向请求的方法。然而,大多数
现有的用户代理实现将 302 视为 303
响应,对位置字段值执行 GET,不管
原始请求方法。状态码 303 和 307 有
已为希望明确表明哪些服务器添加
预计客户会做出什么样的反应。

303 代表 GET-after-POST。古老的浏览器可能不支持它,所以你可能不想将它用于 GET-after-GET:

注意:许多 HTTP/1.1 之前的用户代理不理解 303
地位。当担心与此类客户端的互操作性时,
可以使用 302 状态代码来代替,因为大多数用户代理都会做出反应
到 302 响应,如此处所述的 303 响应。

用于 POST-after-POST(与用户确认后)。它可以用于 GET-after-GET,但在这种情况下你最好使用 302/303:

如果收到 307 状态代码以响应其他请求
与 GET 或 HEAD 相比,用户代理不得自动重定向
请求,除非它可以被用户确认,因为这可能
更改发出请求的条件。

至于兼容性,如果很大比例(1%?)的用户使用不理解 303 或 307 的损坏代理,即使他们声称支持 HTTP/1.1,我也不会感到惊讶。嗯。

It depends entirely on why you're doing the redirect. I'll assume you've read RFC 2616.

You don't want to use 301 except potentially for things like page-renames. I am not aware of any CMS that does this automatically.

Clients with link editing capabilities ought to automatically
re-link references to the Request-URI to one or more of the new
references returned by the server, where possible.

302 is perfectly fine for a temporary GET-after-GET and is, by default, uncacheable. It should not be used for a GET-after-POST, since it actually means POST-after-POST (after asking the user for confirmation):

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.

303 is for GET-after-POST. Ancient browsers might not support it, so you might not want to use it for GET-after-GET:

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.

307 is for POST-after-POST (after confirming with the user). It can be used for GET-after-GET, but in that case you might as well use 302/303:

If the 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.

As for compatibility, I wouldn't be surprised if a significant percentage (1%?) of users are behind broken proxies that don't understand 303 or 307, even if they claim to support HTTP/1.1. Meh.

泼猴你往哪里跑 2024-11-25 07:01:32

为了节省我大量的打字时间 - 阅读此< /a> 和 这个

注意 - 并非所有 3xx 代码都会进行重定向。但301、302、303和307的语义是相似的。

To save me a lot of typing - read this and this.

NB - not all the 3xx codes do redirection. But the semantics of 301, 302, 303 and 307 are similar.

信愁 2024-11-25 07:01:32

根据 Mozilla 文档

重定向响应(形式为
3xx) 表示该资源
客户要求的已搬迁,并且
服务器无法为其提供服务
直接地。大多数这些回复
包含一些位置信息
告诉在哪里可以找到所请求的内容
资源;那么用户代理经常
无需进一步用户即可检索它
相互作用。最常见的回应
这种类型的有 301 已移动
永久

表明给定的 URI 是 no
不再有效并已移至
另一个地方,以及 302
找到了

这表明该资源有
被暂时转移到另一个地方
地点。

注意:对于网站站长,建议设置301 已移动
永久
重定向
在某个期间将页面移动到另一个 URI
例如,网站重组。那
允许用户通过链接继续访问
到达资源,它也教
搜索引擎和其他服务
资源的新位置,以便
他们可以将元数据传输到
它。添加也很重要
足够的缓存标头到 301
永久移动
响应如此
该信息被缓存
客户端并阻止它进行
对原件提出不必要的要求
获取资源之前的 URI
本身。

有关状态代码的更多详细信息,请参阅W3C。有关不同状态代码如何影响 SEO 的直观表示,请参阅 HTTP 状态 SEO 指南代码

According to the Mozilla docs:

Redirection responses (of the form
3xx) indicate that the resource that
the client requested has moved and the
server is not able to serve it
directly. Most of these responses
contain some location information
telling where to find the requested
resource; user-agents often then
retrieve it without further user
interaction. The most common responses
of this type are 301 Moved
Permanently
,
indicating that the URI given is no
longer valid and has been moved to
another place, and 302
Found

which indicates that the resource has
been temporarily moved to another
place.

Note: For webmasters, it is recommended to set up a 301 Moved
Permanently
redirection when
moving pages to another URI, during a
site reorganization for example. That
allows users following links to still
reach the resource and it also teaches
search engines and other services the
new location of the resource, so that
they can transfer their metadata to
it. It is also important to add
adequate cache headers to the 301
Moved Permanently
response so
that this information is cached by the
client and prevents it from making
unnecessary requests to the original
URI prior to fetching the resource
itself.

More detailed information about status codes can be found from the W3C. For a visual representation of how different status codes affect SEO, see SEO Guide to HTTP Status Codes.

无所谓啦 2024-11-25 07:01:32

响应代码 302。或者至少,这是当您调用 sendRedirect() 时 Java 的 HttpServletResponse 发送的代码。如果 Java 就是这么做的,那么可能是有原因的。

301和302在语义上的唯一区别是301表示“永久重定向”而302表示“临时重定向”。这是否会转化为实践中的任何差异完全取决于实施协议的客户端。

例如,浏览器可以决定,由于 301 是永久性的,因此它只会记住它返回的重定向 URL,而不再实际发送对原始 URL 的请求。但这完全取决于浏览器的实现。

也许一个合理的经验法则是,如果您要将静态内容移动到新位置,请始终使用 301。但是,如果您要发送重定向以响应对服务器上某些动态代码的请求,那么您应该使用代码 302 (或者,307)以确保后续请求仍发送到原始 URL,以防您决定更改/更新动态代码以执行不同的操作。

Response code 302. Or at least, that's what Java's HttpServletResponse sends when you call sendRedirect(). And if that's how Java does it, there's probably a reason.

The only difference between 301 and 302 semantically is that 301 indicates a "permanent redirect" and 302 indicates a "temporary redirect". Whether this translates into any difference in practice is entirely up to the client implementing the protocol.

For instance, the browser could decide that since 301 is permanent it will just remember the redirect URL that it gets back and never actually send the request for the original URL anymore. But that would be entirely up to the browser implementation.

Perhaps a reasonable rule of thumb is that if you are moving static content to a new location, always use 301. But if you are sending the redirect in response to a request to some dynamic code on your server, then you should use code 302 (or alternately, 307) to ensure that subsequent requests are still sent to the original URL in case you ever decide to change/update your dynamic code to do something different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文