http-更改请求网址? [英] http - change request url?

查看:65
本文介绍了http-更改请求网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to change the url of a HTTP-request without redirection?

例如代替:

请求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]

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

I could could respond the file directly, but letting the client know that he got redirected:

单个请求

GET /user/abc123/ HTTP/1.1

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

推荐答案

据我所知,使用HTTP不可能做到这一点.HTTP中的重定向专门意味着客户端应该向第二秒发送请求.

As far as I know, it's not possible to do this with HTTP. Redirection in HTTP specifically means the the client is supposed to send a second a request.

我认为您想要的更像是为某些资源指定规范网址",然后在浏览器的位置栏中显示该规范网址.

I think what you want is more akin to specifying a "canonical url" for some resources, and then having this canonical url displayed in the browsers location bar.

RFC 6596 指定了一种通过< link rel指定规范网址的方法="canonical"> .但是,它没有指定浏览器应如何处理它(如果有的话).Google使用它可以更好地选择要索引的网址.

RFC 6596 specifies a way to specify canonical urls with <link rel="canonical">. However, it does not specify what a browser should do with it, if anything. Google uses it to make better choices about which urls to index.

除了使用< link> 标记外,还可以通过HTTP Link 标头指定资源之间的关系,即 Link:</better-url> ;;rel = canonical .请参阅 http://www.w3.org/wiki/LinkHeader .我不确定这是否会被Google接受.该页面位于 http://support.google.com/webmasters/bin/answer.py?hl=zh-CN&answer=139394 并未提及Google支持.就像几乎任何链接标记一样,浏览器肯定会忽略它,样式表是一个明显的例外.

Other than using <link> tags, it's also possible to specify relationships between resources via the HTTP Link header, i.e. Link: </better-url>; rel=canonical. See http://www.w3.org/wiki/LinkHeader . I'm not sure if this would be picked up by Google though. The page at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 doesn't mention Google supports it. Browsers surely will disregard it, as they do with practically any link tag, stylesheets being the notable exception.

如果所讨论的内容是HTML文档,则可以使用 HTML5历史记录API.具体来说,请使用 history.replaceState 方法.我认为其他类型的内容无法实现类似的目的.

If the content in question is a HTML document, you could use the HTML5 history API for this. Specifically, use the history.replaceState method. I don't think achieving something similar is possible with other types of content.

Content-Location 标头实际上可能非常符合您的要求.从HTTP 1.1 RFC的14.14节开始:

Content-Location header may actually fit what you want quite well. From section 14.14 of HTTP 1.1 RFC:

当可以从与请求资源的URI分开的位置访问该实体时,可以使用Content-Location实体头字段为消息中包含的实体提供资源位置.服务器应为对应于响应实体的变量提供Content-Location;尤其是在资源具有多个与之相关联的实体,并且这些实体实际上具有可以单独访问它们的单独位置的情况下,服务器应为返回的特定变体提供Content-Location.

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 )

Content-Location的值还定义了实体的基本URI.

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

Content-Location值不能替代原始请求的URI;它仅是请求时与该特定实体相对应的资源位置的声明.如果希望识别特定实体的来源,将来的请求可以将Content-Location URI指定为请求URI.

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

另请参见这是什么目的HTTP标头字段"Content-Location"?

这篇关于http-更改请求网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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