浏览器缓存HTTP 301s多长时间? [英] How long do browsers cache HTTP 301s?

查看:222
本文介绍了浏览器缓存HTTP 301s多长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试HTTP 301永久重定向的问题。经过快速测试后,似乎Safari在重启时清除了301s的缓存,但Firefox没有。

I am debugging a problem with a HTTP 301 Permanent Redirect. After a quick test, it seems that Safari clears its cache of 301s when it is restarted, but Firefox does not.

IE,Chrome,Firefox和Safari什么时候清除它们缓存301s?

When do IE, Chrome, Firefox and Safari clear their cache of 301s?

更新:例如,如果我想将 example1.com 重定向到 example2.com ,但我不小心将其设置为重定向到 example3.com ,这是一个问题。我可以纠正错误,但在此期间访问 example1.com 的任何人都会将错误的重定向缓存到 example3.com ,所以他们将无法访问 example1.com example2.com ,直到他们的缓存被清除了。经过调查,我发现没有设置 Cache-Control Expires 标头。不正确的301响应的标题应如下所示:

UPDATE: For example, if I want to redirect example1.com to example2.com, but I accidentally set it to redirect to example3.com, that is a problem. I can correct the mistake, but anyone who has visited example1.com in the meantime will have cached the incorrect redirect to example3.com, and so they will not be able to reach either example1.com or example2.com until their cache is cleared. Upon investigation, I find that there were no Cache-Control and Expires headers set. The headers for the incorrect 301 response would have been like this:

HTTP/1.1 301 Moved Permanently
Date: Wed, 27 Feb 2013 12:05:53 GMT
Server: Apache/2.2.21 (Unix) DAV/2 PHP/5.3.8
X-Powered-By: PHP/5.3.8
Location: http://example3.com/
Content-Type: text/html

我自己的测试显示:


  • IE7,IE8,Android 2.3.4根本不缓存。

  • Firefox 18.0.2,Safari 5.1.7(在Windows 7上)和Opera 12.14全部缓存,并在浏览器重启时清除缓存。

  • IE10和Chrome 25缓存,但是不清楚浏览器重启,那么什么时候它们会清除?

  • IE7, IE8, Android 2.3.4 do not cache at all.
  • Firefox 18.0.2, Safari 5.1.7 (on Windows 7), and Opera 12.14 all cache, and clear the cache on browser restart.
  • IE10 and Chrome 25 cache, but do not clear on browser restart, so when will they clear?

推荐答案

至少有两个浏览器 - Chrome和Firefox - 将缓存301重定向没有过期日期

At least two browsers - Chrome and Firefox - will cache a 301 redirect with no expiry date.

也就是说,只要浏览器的缓存可以容纳它,它就会保持缓存状态。如果手动清除缓存,或者清除缓存条目以便为新缓存条目腾出空间,它将从缓存中删除。

That is, it will remain cached for as long as the browser's cache can accommodate it. It will be removed from the cache if you manually clear the cache, or if the cache entries are purged to make room for new ones.

您可以至少在Firefox转到 about:cache 并在磁盘缓存下找到它。

You can verify this at least in Firefox by going to about:cache and finding it under disk cache.

我不知道这个行为其他浏览器,如IE10 / IE11。但是,鉴于其他浏览器无限期地缓存它,您无论如何都必须适应这种情况。

I don't know about the behaviour of other browsers, such as IE10/IE11. However, given that other browsers do cache it indefinitely, you will have to accommodate for this anyway.

在所有浏览器中,包括Chrome / Firefox,仍然可以使用标头覆盖此默认行为,如下所述:

In all browsers, including Chrome/Firefox it is still possible to override this default behavior using headers, as described below:

注意:这个答案写于2014年,浏览器行为可能会随着时间而改变。

如果你不这样做希望重定向被缓存

这种无限期缓存只是在没有Cache-Control的情况下这些浏览器的默认缓存头。逻辑是你指定一个永久重定向,而不是给他们任何其他的缓存指令,所以他们会把它当作你想要它无限期缓存的对待。

This indefinite caching is only the default caching by these browsers in the absence of Cache-Control headers. The logic is that you are specifying a "permanent" redirect and not giving them any other caching instructions, so they'll treat it as if you wanted it indefinitely cached.

浏览器仍然尊重Cache-Control和Expires标头,如同指定的那样。

The browsers still honor the Cache-Control and Expires headers like with any other response, if they are specified.

您可以添加标头,例如 Cache -Control:max-age = 3600 到期:星期四,2014年12月1日16:00:00 GMT 到您的301重定向。你甚至可以添加 Cache-Control:no-cache 所以它不会被浏览器永久缓存或 Cache-Control:no-store 因此浏览器甚至无法将其存储在临时存储空间中。

You can add headers such as Cache-Control: max-age=3600 or Expires: Thu, 01 Dec 2014 16:00:00 GMT to your 301 redirects. You could even add Cache-Control: no-cache so it won't be cached permanently by the browser or Cache-Control: no-store so it can't even be stored in temporary storage by the browser.

然而,在我看来,更好的选择是使用302或307重定向。这些并不意味着浏览器或缓存它们是永久重定向,因此不应缓存在缓存控制标头中。

A better alternative in my opinion, however, is to use a 302 or 307 redirect. These don't imply to browsers or caches that they are "permanent" redirects and thus shouldn't be cached in the absense of Cache-Control headers.

对我来说,似乎发布301重定向但将其标记为不可缓存是违反301重定向的精神,即使它在技术上可能有效。 YMMV,您可能会发现永久重定向有时间限制的边缘情况。

To me, it seems like issuing a 301 redirect but marking it as non-cacheable is going against the spirit of what a 301 redirect is for, even though it may be technically valid. YMMV, and you may find edge cases where it makes sense for a "permanent" redirect to have a time limit.

如果您之前发布了301重定向但是想要取消这样做

如果人们仍然在他们的浏览器中拥有缓存301重定向,他们将继续被带到目标页面,无论是否源页面仍然具有重定向。您解决此问题的方法包括:

If people still have the cached 301 redirect in their browser they will continue to be taken to the target page regardless of whether the source page still has the redirect in place. Your options for fixing this include:


  • 最简单和最佳的解决方案是再次发出另一个301重定向。

  • The simplest and best solution is to issue another 301 redirect back again.

浏览器会意识到它被引导回到它之前认为是一个取消委托的URL,这应该会导致它再次重新获取该URL以确认旧的重定向不是还没有。

The browser will realise it is being directed back to what it previously thought was a de-commissioned URL, and this should cause it re-fetch that URL again to confirm that the old redirect isn't still there.

编辑:有些评论对此表示怀疑,见下文。

如果您无法控制上一个重定向目标所在的站点,那么您将无法运气。尝试并请求网站所有者重定向到您。

If you don't have control over the site where the previous redirect target went to, then you are outta luck. Try and beg the site owner to redirect back to you.

此外预防胜于治疗 - 避免301重定向如果您不确定是否要永久取消旧网址的使用。

Also prevention is better than cure - avoid a 301 redirect if you are not sure you want to permanently de-commission the old URL.

这篇关于浏览器缓存HTTP 301s多长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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