我可以将 HTTP 重定向发送到带有锚点的 URL 吗?

发布于 2024-08-01 19:16:15 字数 152 浏览 2 评论 0原文

是否可以将带有 302 状态代码的响应发送到如下网址:

http://mysite.com/something /#别的东西

Is it possible to send a response with 302 status code to a url like this:

http://mysite.com/something/#somethingelse

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

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

发布评论

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

评论(6

婴鹅 2024-08-08 19:16:15

是的。 浏览器不会将哈希值发送到服务器,而不是相反。

Yes. It's the browser which doesn't send the hash to the server, not the other way around.

梦里人 2024-08-08 19:16:15

而原始的 RFC 2616 仅允许 Location 标头中的绝对URI,正如其他(较旧的)答案所解释的那样,当前的RFC 7231 允许任何 URI 引用,可以是完整的 URI(可能包含片段),甚至是相对 URI,根据解析当前有效的 URI。 它甚至显式地描述了重定向期间片段的行为(如果 Location 标头包含该片段,则使用它,如果不包含,则在重定向后继承并应用原始片段)。

是的,你可以,它现在甚至已经正式标准化。

While the original RFC 2616 allowed only absoluteURI in the Location header, as the other (older) answers explain, the current RFC 7231 allows any URI-reference, either a full URI (with fragment possibly included), or even a relative URI, resolved against the current effective URI. And it even explicitly describes the behavior of fragments during redirection (if the Location header includes the fragment, it is used, if not, the original fragment is inherited and applied after the redirection).

I.e. yes, you can, it is even officially standardized now.

漫雪独思 2024-08-08 19:16:15

是的,您可以使用片段标识符。 这是 HTTP 规范中的一个已知错误,已在规范的后续修订中修复。 请参阅 RFC 9110。

Yes, you can use the fragment identifier. This was a known bug in the HTTP spec, fixed in subsequent revisions of the spec. See RFC 9110.

乖乖哒 2024-08-08 19:16:15

遵循 HTTP 规范,Location< /code> 标头字段必须是一个absoluteURI 值。 这是根据 RFC 3986 (他们刚刚将名称从 AbsoluteURIabsolute-URI):

absolute-URI  = scheme ":" hier-part [ "?" query ]

因此理论上不允许片段作为值的一部分。 但浏览器可以处理它。

Following the HTTP specification, the value for the Location header field must be an absoluteURI value. And that is according to the RFC 3986 (they just changed the name from absoluteURI to absolute-URI):

absolute-URI  = scheme ":" hier-part [ "?" query ]

So theoretically the fragment is not allowed as part of the value. But the browsers can handle it.

極樂鬼 2024-08-08 19:16:15

严格阅读 RFC2616 不允许 Location 标头值中存在片段,因为它们不是绝对 URI 的一部分。 然而,随着 IETF 的 HTTP 重写草案 此问题已修复

最近 Julian 比较了浏览器如何在 Location 中处理 URI 片段(这就是 HTML 锚标记处理的内容)标题: http://www.greenbytes.de/tech/tc2231/redirects.html< /a>

所以答案是:是的,您可以将片段放入位置标头 URI 中。

With strict reading RFC2616 does not allow fragments in Location header values, since they are not part of absolute URIs. However, with the IETF's HTTP rewrite draft this was fixed.

Recently Julian put up a comparison how browsers handle URI fragments (that's what the HTML anchor tags deal with) in the Location header: http://www.greenbytes.de/tech/tc2231/redirects.html

So the answer ist: Yes, you can put fragments in Location header URIs.

追我者格杀勿论 2024-08-08 19:16:15

从 PHP 中执行此操作似乎没有问题:

Header(
    "Location: http://en.wikipedia.org/wiki/HTTP#Status_codes",
    true,
    302
);

There appears to be no problem in doing so from PHP:

Header(
    "Location: http://en.wikipedia.org/wiki/HTTP#Status_codes",
    true,
    302
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文