gpt4 book ai didi

node.js - http - 更改请求网址?

转载 作者:搜寻专家 更新时间:2023-10-31 23:55:27 25 4
gpt4 key购买 nike

是否可以在不重定向的情况下更改 HTTP 请求的 url?

例如代替:

请求1

GET /user/abc123/ HTTP/1.1

HTTP/1.1 301 Moved Permanently
Location: /files/abc123

请求2

GET /files/abc123 HTTP/1.1

HTTP/1.1 200 OK
.
.
[filecontent]

我可以直接响应文件,但让客户知道他被重定向了:

单个请求

GET /user/abc123/ HTTP/1.1

HTTP/1.1 200 OK
Location: /files/abc123
.
.
[filecontent]

最佳答案

据我所知,使用 HTTP 无法做到这一点。 HTTP 中的重定向具体意味着客户端应该发送第二个请求。

我认为您想要的更类似于为某些资源指定“规范 url”,然后在浏览器地址栏中显示此规范 url。

RFC 6596指定一种使用 <link rel="canonical"> 指定规范网址的方法.但是,它没有指定浏览器应该用它做什么,如果有的话。 Google 使用它来更好地选择将哪些 URL 编入索引。

除了使用 <link>标签,也可以通过 HTTP Link 指定资源之间的关系 header ,即 Link: </better-url>; rel=canonical .参见 http://www.w3.org/wiki/LinkHeader .不过,我不确定这是否会被谷歌接受。页面位于 http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394没有提到谷歌支持它。浏览器肯定会忽略它,因为它们实际上会忽略任何链接标记,样式表是一个明显的异常(exception)。

如果相关内容是 HTML 文档,您可以使用 HTML5 history API为了这。具体来说,使用 history.replaceState方法。我认为其他类型的内容无法实现类似的效果。

编辑

Content-Location header 实际上可能非常适合您想要的。来自 HTTP 1.1 RFC 的第 14.14 节:

The Content-Location entity-header field MAY be used to supply the resource location for the entity enclosed in the message when that entity is accessible from a location separate from the requested resource's URI. A server SHOULD provide a Content-Location for the variant corresponding to the response entity; especially in the case where a resource has multiple entities associated with it, and those entities actually have separate locations by which they might be individually accessed, the server SHOULD provide a Content-Location for the particular variant which is returned.

  Content-Location = "Content-Location" ":"
                     ( absoluteURI | relativeURI )

The value of Content-Location also defines the base URI for the entity.

The Content-Location value is not a replacement for the original requested URI; it is only a statement of the location of the resource corresponding to this particular entity at the time of the request. Future requests MAY specify the Content-Location URI as the request- URI if the desire is to identify the source of that particular entity.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

另见 What is the purpose of the HTTP header field “Content-Location”?

关于node.js - http - 更改请求网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16437950/

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