处理301重定向时的客户端Web浏览器行为 [英] Client Web Browser Behavior When Handling 301 Redirect

查看:285
本文介绍了处理301重定向时的客户端Web浏览器行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RFC似乎暗示客户端应永久缓存响应:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html


10.3.2 301永久移动



请求的资源已被
分配一个新的永久性URI,任何
将来对这个资源的引用
应该使用一个返回的URI。
具有链接编辑功能的客户
应自动将
引用与Request-URI重新链接到一个
或更多由服务器返回
的新引用,在可能的情况。这个
响应是可缓存的,除非另有指示


新的永久URI应该由响应中的位置字段给出

除非请求方法是HEAD(
),否则响应的实体SHOULD
包含一个带有
超链接到新URI的简短超文本记录。



如果在
中接收到301状态码,而不是GET
或HEAD,则用户代理不能
自动重定向请求
,除非可以由
用户确认,因为这可能会改变发出
请求的
条件。

 注意:在
接收到301状态码之后自动重定向POST请求时,一些现有的HTTP / 1.0用户代理
将错误地将其更改为GET请求。


我很难找到任何具体的浏览器文档主要的浏览器,说明他们如何处理这些。

我已经开始挖掘Firefox的源代码,但很快就迷失了方向。



对于哪个(如果有的话)浏览器,以下情况是否正确,并且Firefox或IE的权威性文档如何表示?



第一次:




  • 1.1:用户输入链接到站点A ,或点击指向站点A的链接
  • 1.2:浏览器首次解释站点A的链接,没有缓存。发送GET到站点A.
  • 1.2:站点A回应301重定向到站点B
  • 1.3:浏览器发送GET到站点B.


任何后续时间:

  • 2.2:用户点击指向网站A的链接
  • 2.2:浏览器发现,由于过去的301重定向,网站A现在应该是网站B. 2.3:在网站A没有发起任何请求,浏览器在网站B启动GET。

    解决方案

    我做了一些测试,发现有些浏览器会缓存301结果:

     
    缓存301结果,将来会跳过联系旧地址?

    Internet Explorer 7 no
    Firefox 3.0没有
    Chrome 4.0是
    Opera 10.01是google.com,no是www.rnhart.net



    我如何测试



    我使用以下两个301结果来测试:




    • google.com返回301给www.google.com

    • www.rnhart.net返回301到rnhart.net



    我在自己的电脑上启动了代理服务器( Proxomitron Naoko 4.2关闭所有过滤器)。在每个浏览器中,我将代理设置指向我自己的计算机。我清除了浏览器的缓存,然后我多次访问了旧地址,并在代理服务器的日志窗口中查看了浏览器发出的请求。



    第一次老地址被访问,代理日志显示旧地址请求,301响应和新地址请求。如果再次访问旧地址,则日志要么显示相同的请求(301没有被缓存),要么只显示新的地址请求(301被缓存)。



    我测试了在地址框中输入旧地址,从书签访问旧地址,并从页面上的链接访问旧地址。无论访问地址如何,每个浏览器的工作方式都是一样的。






    <我>在调查类似的Super用户问题:浏览器是否更改URL保存书签响应301重定向?]


    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:

  • 2.2: User clicks on a link directed at Site A
  • 2.2: Browser sees that, due to a past 301 redirect, Site A should now be Site B.
  • 2.3: Without initiating any request whatsoever at Site A, browser initiates GET at Site B.

    解决方案

    I preformed some tests and found some browsers do cache the 301 result:

    Caches 301 result and skips contacting old address in future?
    
      Internet Explorer 7   no
      Firefox 3.0           no
      Chrome 4.0            yes
      Opera 10.01           yes for google.com, no for www.rnhart.net
    

    How I tested

    I used the following two 301 results to test with:

    • google.com returns a 301 to www.google.com
    • www.rnhart.net returns a 301 to rnhart.net

    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?]

    这篇关于处理301重定向时的客户端Web浏览器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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