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

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

问题描述

我正在调试 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 何时清除 301 缓存?

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

更新:例如,如果我想将 example1.com 重定向到 example2.com,但我不小心将其设置为重定向到 example3.com,这是一个问题.我可以更正错误,但是在此期间访问过 example1.com 的任何人都会将错误的重定向缓存到 example3.com,因此他们将无法访问 example1.comexample2.com 直到它们的缓存被清除.经过调查,我发现没有设置 Cache-ControlExpires 标头.不正确的 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 缓存,但在浏览器重启时不会清除,那么它们什么时候会清除?

推荐答案

在没有另外指定的缓存控制指令的情况下,301 重定向默认为在没有任何到期日期的情况下缓存.

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

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 并在磁盘缓存下找到它来验证这一点.它在其他浏览器中也是如此,包括 Chrome 和基于 Chromium 的 Edge,尽管它们没有用于检查缓存的 about:cache.

You can verify this at least in Firefox by going to about:cache and finding it under disk cache. It works this way in other browsers including Chrome and the Chromium based Edge, though they don't have an about:cache for inspecting the cache.

在所有浏览器中,仍然可以使用缓存指令覆盖此默认行为,如下所述:

In all browsers it is still possible to override this default behavior using caching directives, as described below:

如果您不想缓存重定向

这种无限期缓存只是这些浏览器的默认缓存,没有其他指定的标头.逻辑是您指定了一个永久的"重定向而不给他们任何其他缓存指令,因此他们会将其视为您希望它无限期缓存.

This indefinite caching is only the default caching by these browsers in the absence of headers that specify otherwise. 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=3600Expires: Thu, 01 Dec 2014 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 重定向可能是更好的选择.发出 301 重定向但将其标记为不可缓存与 301 重定向的精神背道而驰,即使它在技术上是有效的.YMMV,您可能会发现边缘情况对永久"文件有意义.重定向有时间限制.请注意,浏览器默认不会缓存 302 和 307 重定向.

Though, if you don't want your redirect to be permanent, it may be a better option to use a 302 or 307 redirect. 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 is technically valid. YMMV, and you may find edge cases where it makes sense for a "permanent" redirect to have a time limit. Note that 302 and 307 redirects aren't cached by default by browsers.

如果您之前发出过 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:

  • 一个简单的解决方案是再次发出另一个重定向.

  • A simple solution is to issue another redirect back again.

如果浏览器在重定向期间第二次被定向回同一个 URL,它应该再次从源中获取它,而不是从缓存中再次重定向,以避免重定向循环.对此答案的评论表明这现在适用于所有主要浏览器 - 但可能有一些次要浏览器不适用.

If the browser is directed back to a same URL a second time during a redirect, it should fetch it from the origin again instead of redirecting again from cache, in an attempt to avoid a redirect loop. Comments on this answer indicate this now works in all major browsers - but there may be some minor browsers where it doesn't.

如果您无法控制前一个重定向目标所在的站点,那么您就不走运了.尝试请求网站所有者重定向回您.

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

预防胜于治疗 - 如果您不确定要永久停用旧网址,请避免使用 301 重定向.

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

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

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