gpt4 book ai didi

redirect - URL 片段和 302 重定向

转载 作者:行者123 更新时间:2023-12-03 04:29:51 28 4
gpt4 key购买 nike

众所周知,URL 片段(# 后面的部分)不会发送到服务器。

我确实想知道当涉及服务器重定向(通过 HTTP 状态 302 和 Location: header )时片段如何工作。

我的问题实际上有两个:

  1. 如果原始 URL 有一个片段 (/original.php#foo),并且重定向到 /new.php,则该片段是否原始 URL 的一部分就这样丢失了?或者它有时会应用于新的 URL 吗?
    在这种情况下,新的 URL 会是 /new.php#foo 吗?

  2. 无论原始 URL 是什么,如果服务器重定向到带有片段 (/new.php#foo) 的新 URL,该片段是否会“受到尊重”?或者服务器真的根本没有必要干扰该片段——浏览器是否会因此通过简单地访问 /new.php 来忽略它??

最佳答案

更新 2022 年 9 月 22 日:

RFC 9110/STD 97 HTTP Semantics (废弃了 7231(和其他)),已于 2022 年 6 月作为互联网标准发布。新编号 Section 10.2.2 Location 中的措辞与之前/之后保持相同。

2014 年 6 月 27 日更新:

RFC 7231, Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content ,已作为拟议标准发布。来自 Changelog :

The syntax of the Location header field has been changed to allow allURI references, including relative references and fragments, alongwith some clarifications as to when use of fragments would not beappropriate. (Section 7.1.2)

Section 7.1.2. Location 中的要点:

If the Location value provided in a 3xx (Redirection) response doesnot have a fragment component, a user agent MUST process theredirection as if the value inherits the fragment component of the URIreference used to generate the request target (i.e., the redirectioninherits the original reference's fragment, if any).

For example, aGET request generated for the URI reference"http://www.example.org/~tim" might result in a 303 (See Other)response containing the header field:

Location: /People.html#tim

which suggests that the user agent redirect to"http://www.example.org/People.html#tim"

Likewise, a GET request generated for the URI reference"http://www.example.org/index.html#larry" might result in a 301 (MovedPermanently) response containing the header field:

Location: http://www.example.net/index.html

which suggests that the user agent redirect to"http://www.example.net/index.html#larry", preserving the originalfragment identifier.

这应该清楚地回答您的问题。

更新结束

这是 current HTTP specification 的一个未解决(未指定)问题。 。 IETF httpbis working group 的两期中对此进行了讨论:

#6 允许 Location header 中存在片段。 #43 是这样说的:

I just tested this with various browsers.

  • Firefox and Safari use the fragment in the location header.
  • Opera uses the fragment from the source URI, when present, otherwise the fragment from the redirect location
  • IE (8) ignores the fragment in the location URI, thus will use the fragment from the source URI, when present

Proposal:

"Note: the behavior when fragment identifiers from the original URI and the redirect need to be combined is undefined; current User Agents indeed differ on what fragment takes precedence."

[...]

It appears that IE8 does use the fragment idenfitier from Location (the behavior I saw might be limited to localhost).

Thus we seem to have consistent behavior for Safari/IE/Firefox/Chrome (just tested), in that the fragment from the Location header gets used, no matter what the original URI was.

I therefore change my proposal to document that as expected behavior.

这将为您的问题提供最具浏览器兼容性和面向 future 的答案(因为这个问题最终将得到标准化):

A:原始 URL 中的片段将被丢弃。

B: 来自 Location header 的片段受到尊重。

关于redirect - URL 片段和 302 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2286402/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com