处理 301 重定向时客户端 Web 浏览器的行为
RFC 似乎建议客户端应该永久缓存响应: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
10.3.2 301永久移动
请求的资源已 分配一个新的永久 URI 和任何 未来对该资源的引用 应使用返回的 URI 之一。 具有链接编辑功能的客户端 应该自动重新链接 对 Request-URI 的引用之一 或更多返回的新引用 在可能的情况下由服务器执行。这 除非另有说明,响应是可缓存的 否则。
应该给出新的永久 URI 通过响应中的位置字段。 除非请求方法是HEAD, 响应的实体应该 包含一个简短的超文本注释 指向新 URI 的超链接。
如果收到 301 状态码 对 GET 以外的请求的响应 或 HEAD,用户代理不得 自动重定向请求 除非能够得到相关部门的确认 用户,因为这可能会改变 提出请求的条件 已发布。
注意:之后自动重定向 POST 请求时 收到 301 状态代码,一些现有的 HTTP/1.0 用户代理 会错误地将其更改为 GET 请求。
我很难找到任何主要浏览器的具体浏览器文档来说明它们如何处理这些问题。
我已经开始深入研究 Firefox 的源代码,但很快就迷失了方向。
对于哪种(如果有)浏览器,以下情况是否属实?Firefox 或 IE 是否有明确的文档进行了说明?:
第一次:
- 1.1:用户输入指向站点 A 的链接,或者单击指向站点 A 的链接
- 1.2:浏览器第一次解释站点A的链接,没有缓存。将 GET 发送到站点 A。
- 1.2:站点 A 响应 301 重定向到站点 B
- 1.3:浏览器向站点B发送GET。
任何后续时间:
The RFC seems to suggest that the client should permanently cache the response:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
10.3.2 301 Moved Permanently
The requested resource has been
assigned a new permanent URI and any
future references to this resource
SHOULD use one of the returned URIs.
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. This
response is cacheable unless indicated
otherwise.The new permanent URI SHOULD be given
by the Location field in the response.
Unless the request method was HEAD,
the entity of the response SHOULD
contain a short hypertext note with a
hyperlink to the new URI(s).If the 301 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.Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request.
I'm having a hard time finding concrete browser documentation for any major browser that states how they handle these.
I've started digging through the source code of firefox, but quickly got lost.
Is the following scenario true for which (if any) browsers, and is there definitive documentation for either Firefox or IE that states as much?:
First Time Around:
- 1.1: User enters link to site A, or clicks on a link directed at Site A
- 1.2: Browser interprets link at Site A, first time, no cache. Sends GET to Site A.
- 1.2: Site A responds with 301 Redirect to Site B
- 1.3: Browser sends GET to Site B.
Any Subsequent Times Around:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我进行了一些测试,发现某些浏览器确实缓存了 301 结果:
我如何测试
我使用以下两个 301 结果进行测试:
我在自己的计算机上启动了代理服务器(Proxomitron Naoko 4.2,所有过滤器均已关闭)。在每个浏览器中,我将代理设置设置为指向我自己的计算机。我清除了浏览器的缓存,然后多次访问旧地址,并查看代理服务器的日志窗口以查看浏览器发出了哪些请求。
第一次访问旧地址时,代理日志会显示旧地址请求、301 响应和新地址请求。如果再次访问旧地址,日志要么显示同一组请求(301 未缓存),要么仅显示新地址请求(301 已缓存)。
我测试了在地址框中输入旧地址、从书签访问旧地址以及从页面上的链接访问旧地址。无论如何访问地址,每个浏览器都以相同的方式工作。
[我在调查类似的超级用户问题时发现了这个问题:浏览器是否会更改已保存书签的 URL 以响应 301 重定向?]
I preformed some tests and found some browsers do cache the 301 result:
How I tested
I used the following two 301 results to test with:
I started a proxy server on my own computer (Proxomitron Naoko 4.2 with all filters turned off). In each browser, I set the proxy settings to point to my own computer. I cleared the browser's cache, then I visited the old address multiple times and looked in the proxy server's log window to see what requests the browser made.
The first time the old address is visited, the proxy log shows the old address request, the 301 response, and the new address request. If the old address is visited again, the log either showed the same set of requests (the 301 wasn't cached), or it showed only the new address request (the 301 was cached).
I tested entering the old address in the address box, accessing the old address from a bookmark, and accessing the old address from a link on a page. Each browser worked the same way no matter how the address was accessed.
[I found this question while investigating a similar Super User question: Do browsers change URLs of saved bookmarks in response to 301 redirection?]
您可以使用此解决方法:
为用户设置
302
重定向,为搜索引擎设置301
。在服务器端,只需检查用户代理即可。如果是机器人,请执行
301
重定向。否则,执行302
。这不是“黄金之路”,但效果很好
You may use this workaround:
Make
302
redirect for users and301
only for search engines.On the server side, just check for the user agent. If it is a bot, do a
301
redirect. Otherwise, do302
.It is not the "golden way", but it works great