浏览器会缓存 HTTP 301 多长时间?
我正在调试 HTTP 301 永久重定向的问题。经过快速测试,Safari 在重新启动时似乎会清除 301 的缓存,但 Firefox 不会。
IE、Chrome、Firefox 和 Safari 何时清除 301 缓存?
例如,如果我想将 1.example
重定向到 2.example
,但我不小心将其设置为重定向到 3.example
,那么是一个问题。我可以纠正错误,但同时访问过 1.example
的任何人都会缓存到 3.example
的错误重定向,因此他们将无法到达 1.example
或 2.example
直到其缓存被清除。经过调查,我发现没有设置 Cache-Control
和 Expires
标头。错误的 301 响应的标头如下所示:
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://3.example/
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 缓存,但浏览器重启时不会清除,什么时候会清除?
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.
When do IE, Chrome, Firefox and Safari clear their cache of 301s?
For example, if I want to redirect 1.example
to 2.example
, but I accidentally set it to redirect to 3.example
, that is a problem. I can correct the mistake, but anyone who has visited 1.example
in the meantime will have cached the incorrect redirect to 3.example
, and so they will not be able to reach either 1.example
or 2.example
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://3.example/
Content-Type: text/html
My own tests show that:
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
如果没有另外指定的缓存控制指令,301 重定向默认会被缓存,没有任何到期日期。
也就是说,只要浏览器的缓存可以容纳它,它就会保持缓存状态。如果您手动清除缓存,或者清除缓存条目以为新条目腾出空间,则它将从缓存中删除。
您至少可以在 Firefox 中验证这一点,方法是转到
about:cache
并在磁盘缓存下找到它。它在其他浏览器中以这种方式工作,包括 Chrome 和基于 Chromium 的 Edge,尽管它们没有用于检查缓存的about:cache
。在所有浏览器中,仍然可以使用缓存指令覆盖此默认行为,如下所述:
如果您不希望缓存重定向
这种无限期缓存只是默认 在没有其他指定标头的情况下由这些浏览器进行缓存。逻辑是,您指定“永久”重定向,并且不向他们提供任何其他缓存指令,因此他们会将其视为您希望无限期缓存。
浏览器仍然会像任何其他响应一样尊重 Cache-Control 和 Expires 标头(如果指定了它们)。
您可以向 301 重定向添加诸如
Cache-Control: max-age=3600
或Expires: Thu, 01 Dec 2014 16:00:00 GMT
之类的标头。您甚至可以添加Cache-Control: no-cache
这样它就不会被浏览器永久缓存,或者Cache-Control: no-store
所以它甚至不能由浏览器存储在临时存储器中。不过,如果您不希望重定向是永久性的,那么使用 302 或 307 重定向可能是更好的选择。发出 301 重定向但将其标记为不可缓存违反了 301 重定向的精神,尽管它在技术上是有效的。 YMMV,您可能会发现边缘情况,其中“永久”重定向有时间限制是有意义的。请注意,浏览器默认不会缓存 302 和 307 重定向。
如果您之前发出了 301 重定向,但想要撤消该操作
如果人们的浏览器中仍然有缓存的 301 重定向,他们将继续被带到目标页面,无论源页面是否仍然有重定向到位。解决此问题的选项包括:
一个简单的解决方案是再次发出另一个重定向。
如果浏览器在重定向期间第二次被定向回同一 URL,则它应该再次从源获取该 URL,而不是再次从缓存重定向,以避免重定向循环。对此答案的评论表明这现在适用于所有主要浏览器 - 但可能有一些次要浏览器不适用。
如果您无法控制先前重定向目标所前往的网站,那么您就不走运了。尝试请求网站所有者重定向回您。
预防胜于治疗 - 如果您不确定是否要永久停用旧 URL,请避免 301 重定向。
In the absense of cache control directives that specify otherwise, a 301 redirect defaults to being cached without any 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.
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 anabout:cache
for inspecting the cache.In all browsers it is still possible to override this default behavior using caching directives, as described below:
If you don't want the redirect to be cached
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.
The browsers still honor the Cache-Control and Expires headers like with any other response, if they are specified.
You can add headers such as
Cache-Control: max-age=3600
orExpires: Thu, 01 Dec 2014 16:00:00 GMT
to your 301 redirects. You could even addCache-Control: no-cache
so it won't be cached permanently by the browser orCache-Control: no-store
so it can't even be stored in temporary storage by the browser.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.
If you previously issued a 301 redirect but want to un-do that
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.
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.
Prevention is better than cure - avoid a 301 redirect if you are not sure you want to permanently de-commission the old URL.
从 Chrome 71 开始,
要清除永久重定向,请转到 chrome://settings/clearBrowserData,然后仅清除“缓存的图像和文件”即可清除重定向。
Chrome 48-70
转到 chrome://net-internals。在顶部红色状态栏右侧,点击向下箭头▼打开下拉菜单,在“工具”组下选择“清除缓存”。
从版本 48 开始,这是我清除缓存的 301 的唯一方法。
From Chrome 71
To clear a permanent redirect, go to chrome://settings/clearBrowserData and from there only clearing "cached images and files" cleared the redirect.
Chrome 48-70
Go to chrome://net-internals. On the right of the top red status bar, click on the down arrow ▼ to open the drop-down menu, and under the "Tools" group, choose "Clear cache".
As of version 48, this was the only thing that worked for me to clear a cached 301.
一个可以帮助那些迫切希望摆脱重定向缓存的人的答案:
Chrome 无限缓存 301 重定向(在本地磁盘缓存中)。要清除此缓存:
F12
),F5
)。当一切正常时,您可以取消选中“禁用缓存”,一切将继续按预期工作。
An answer that helps those who desperately want to get rid of the redirect cache:
Chrome caches the 301 redirect infinitely (in the local disk cache). To clear this cache:
F12
)F5
)When everything is okay, you can uncheck "Disable cache" and everything will continue to work as expected.
有一种非常简单的方法可以删除 http 重定向的浏览器缓存,例如 301、307 等。
您可以在 Chrome 的开发人员控制台中打开网络面板。选择网络通话。右键单击它,然后单击清除浏览器缓存以删除缓存的重定向。
There is a very simple way to remove browser cache for http redirects e.g. 301, 307 etc.
You can open network panel in developer console in chrome. Select the network call. Right click on it and then click on Clear Browser Cache to remove the cached redirection.
让用户在该网址上提交帖子表单,缓存的重定向就会消失:)
Make the user submit a post form on that url and the cached redirect is gone :)
301
是每个 HTTP RFC 的可缓存响应,浏览器将根据响应中的 HTTP 缓存标头对其进行缓存。使用 FireBug 或 Charles 检查响应标头以了解响应缓存的确切持续时间。如果您想控制缓存持续时间,可以使用 HTTP 响应标头
Cache-Control
和Expires
来执行相同操作。或者,如果您根本不想缓存301
响应,请使用以下标头。301
is a cacheable response per HTTP RFC and browsers will cache it depending on the HTTP caching headers you have on the response. Use FireBug or Charles to examine response headers to know the exact duration the response will be cached for.If you would like to control the caching duration, you can use the the HTTP response headers
Cache-Control
andExpires
to do the same. Alternatively, if you don't want to cache the301
response at all, use the following headers.确认的!!让用户向受影响的 url 提交 post 请求,并且缓存的重定向被遗忘。
快速获胜的方法是在浏览器控制台中输入以下内容:
fetch('example.com/affected/link', {method: 'post'}).then(( ) => {})
如果您了解受影响的浏览器(尤其是在开发过程中),则很有用。
或者,如果您可以访问之前的 301 重定向页面,那么您可以将此脚本添加到页面中,并且无论何时访问它,缓存的 301 都会被忘记。
Confirmed!! make the user submit a post request to the affected url and the cached redirect is forgotten.
A quick win would be to enter this in the browser console if you can:
fetch('example.com/affected/link', {method: 'post'}).then(() => {})
Useful if you know the affected browser (especially during development).
Alternatively, if you have access to the previous 301 redirect page, then you can add this script to the page and anytime it is visited, the cached 301 will be forgotten.
我将发布对我有帮助的答案:
转到网址:
它应该调用弹出窗口,然后..
缓存的图像和文件
。从头开始
I will post answer that helped me:
go to url:
it should invoke popup and then..
cached images and files
.from beginning
出于测试目的(以避免缓存重定向),人们可以打开新私人窗口:点击
CTRL+SHIFT+
N
[如果您使用 Mozilla,请使用P
]For testing purposes (to avoid cached redirects), people can open NEW PRIVATE WINDOW: click
CTRL+SHIFT+
N
[if you use Mozilla, useP
]作为@thomasrutter的回答
事实上,这意味着:
a.example
301 到b.example
删除
a.example
的 301将
b.example
301 添加到a.example
然后就可以了。
as answer of @thomasrutter
In fact, this means:
a.example
301 tob.example
delete
a.example
's 301add
b.example
301 toa.example
Then it works.
我有一个适用于所有主要浏览器(最新版本)的简单解决方案,包括 IE、Chrome 和 FF
I have simple solution that worked on all major browser (latest version), includes IE, Chrome and FF
在最新的 Google Chrome 版本 79 上,您可以使用
chrome://net-internals
并从左侧面板中选择 DNS,然后点击清除主机缓存按钮
On the latest Google Chrome Version 79, you can use the
chrome://net-internals
and select on DNS from the left panel, then tap the Clear host cache button
正如其他答案所示,缓存在浏览器中的持续时间可能是无限期的。这是极其
危险烦人的。所以不要这样做。至少添加缓存标头。在 htaccess 中,我总是这样做,根本没有缓存:编辑:
如果您过去没有缓存 301 重定向,则必须从目标重定向回源。示例:
如果你有这个,
你需要把这个
As the other answers show caching may be of indefinite duration in browsers. This is extremely
dangerousannoying. So don't do it. At least add cache headers. In htaccess I always do it this way with no caching at all:Edit:
If you didn't had no caching of 301 redirects in the past, you must redirect back to the source from the target. Example:
If you had this
You need to put this
为了解决本地主机地址的问题,我更改了站点运行的端口号。这适用于 Chrome 版本 73.0.3683.86。
To solve the issue for a localhost address I changed the port number the site ran under. This worked on Chrome version 73.0.3683.86.
使用隐身/InPrivate 模式测试您的重定向,以便当您关闭浏览器时,它将刷新该缓存,并且重新打开窗口将不包含缓存。
Test your redirects using incognito/InPrivate mode so when you close the browser it will flush that cache and reopening the window will not contain the cache.
移动 Chrome 的一个技巧(此处为版本 93):尝试在“桌面站点”模式下打开 url - 这在我的情况下删除了永久缓存的重定向。
A trick for mobile Chrome (version 93 here): try opening the url in "desktop site" mode - this removed a permanent cached redirect in my situation.
这是有效的答案
This is the working answer
在你的 .htaccess 文件中尝试这个:
try this one in your .htaccess File:
要禁用重定向,请将此代码放入重定向站点的索引文件中
To disable redirecting, put this code in index file of redirecting site