浏览器会缓存 HTTP 301 多长时间?

发布于 2025-01-02 13:55:07 字数 904 浏览 4 评论 0原文

我正在调试 HTTP 301 永久重定向的问题。经过快速测试,Safari 在重新启动时似乎会清除 301 的缓存,但 Firefox 不会。

IE、Chrome、Firefox 和 Safari 何时清除 301 缓存?

例如,如果我想将 1.example 重定向到 2.example,但我不小心将其设置为重定向到 3.example,那么是一个问题。我可以纠正错误,但同时访问过 1.example 的任何人都会缓存到 3.example 的错误重定向,因此他们将无法到达 1.example2.example 直到其缓存被清除。经过调查,我发现没有设置 Cache-ControlExpires 标头。错误的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

鸵鸟症 2025-01-09 13:55:07

如果没有另外指定的缓存控制指令,301 重定向默认会被缓存,没有任何到期日期。

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

您至少可以在 Firefox 中验证这一点,方法是转到 about:cache 并在磁盘缓存下找到它。它在其他浏览器中以这种方式工作,包括 Chrome 和基于 Chromium 的 Edge,尽管它们没有用于检查缓存的 about:cache

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

如果您不希望缓存重定向

这种无限期缓存只是默认 在没有其他指定标头的情况下由这些浏览器进行缓存。逻辑是,您指定“永久”重定向,并且不向他们提供任何其他缓存指令,因此他们会将其视为您希望无限期缓存。

浏览器仍然会像任何其他响应一样尊重 Cache-Control 和 Expires 标头(如果指定了它们)。

您可以向 301 重定向添加诸如 Cache-Control: max-age=3600Expires: 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 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:

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 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.

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.

余生共白头 2025-01-09 13:55:07

从 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.

陈独秀 2025-01-09 13:55:07

一个可以帮助那些迫切希望摆脱重定向缓存的人的答案:

Chrome 无限缓存 301 重定向(在本地磁盘缓存中)。要清除此缓存:

  • 打开DevTools(按F12),
  • 网络选项卡上 选中“禁用缓存”复选框
  • 保持 DevTools 打开并重新加载页面(按 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:

  • open your DevTools (press F12)
  • on the Network tab check the "Disable cache" checkbox
  • keep DevTools open and reload the page (press F5)

When everything is okay, you can uncheck "Disable cache" and everything will continue to work as expected.

青春有你 2025-01-09 13:55:07

有一种非常简单的方法可以删除 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.

network call context menu

青巷忧颜 2025-01-09 13:55:07

让用户在该网址上提交帖子表单,缓存的重定向就会消失:)

<body onload="document.forms[0].submit()">
<form action="https://forum.pirati.cz/unreadposts.html" method="post">
    <input type="submit" value="fix" />
</form>
</body>

Make the user submit a post form on that url and the cached redirect is gone :)

<body onload="document.forms[0].submit()">
<form action="https://forum.pirati.cz/unreadposts.html" method="post">
    <input type="submit" value="fix" />
</form>
</body>
她说她爱他 2025-01-09 13:55:07

301 是每个 HTTP RFC 的可缓存响应,浏览器将根据响应中的 HTTP 缓存标头对其进行缓存。使用 FireBug 或 Charles 检查响应标头以了解响应缓存的确切持续时间。

如果您想控制缓存持续时间,可以使用 HTTP 响应标头 Cache-ControlExpires 来执行相同操作。或者,如果您根本不想缓存 301 响应,请使用以下标头。

Cache-Control: no-store, no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT

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 and Expires to do the same. Alternatively, if you don't want to cache the 301 response at all, use the following headers.

Cache-Control: no-store, no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT
拥抱影子 2025-01-09 13:55:07

确认的!!让用户向受影响的 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.

同展鸳鸯锦 2025-01-09 13:55:07

我将发布对我有帮助的答案:

转到网址:

chrome://settings/clearBrowserData

它应该调用弹出窗口,然后..

  • 仅选择:缓存的图像和文件
  • 选择时间框:从头开始

I will post answer that helped me:

go to url:

chrome://settings/clearBrowserData

it should invoke popup and then..

  • select only: cached images and files.
  • select time box : from beginning
冰之心 2025-01-09 13:55:07

出于测试目的(以避免缓存重定向),人们可以打开新私人窗口:点击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, use P]

你怎么这么可爱啊 2025-01-09 13:55:07

作为@thomasrutter的回答

如果您之前发出了 301 重定向但想要撤消该操作

如果人们的浏览器中仍然有缓存的 301 重定向,无论源页面是否仍然有重定向,他们都会继续被带到目标页面。解决此问题的选项包括:

<块引用>

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

<块引用>

浏览器将意识到它被定向回之前认为已停用的 URL,这应该会导致它再次重新获取该 URL,以确认旧的重定向不再存在。

<块引用>

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

事实上,这意味着:

  1. a.example 301 到 b.example

  2. 删除 a.example 的 301

  3. b.example 301 添加到 a.example

然后就可以了。

as answer of @thomasrutter

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:

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

The browser will realise it is being directed back to what it previously thought was a decommissioned 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.

In fact, this means:

  1. a.example 301 to b.example

  2. delete a.example 's 301

  3. add b.example 301 to a.example

Then it works.

作死小能手 2025-01-09 13:55:07

我有一个适用于所有主要浏览器(最新版本)的简单解决方案,包括 IE、Chrome 和 FF

  1. Ctrl + Shift + Del
  2. -
    1. Chrome:选择“浏览历史记录”和“缓存...”
    2. IE:我保留默认选项“临时 Internet 文件和网站文件”、“Cookie 和网站数据”、“历史记录”
    3. FF:“浏览和下载历史记录”、“缓存”
  3. 单击“删除”
  4. 关闭并重新打开浏览器。应该有效

I have simple solution that worked on all major browser (latest version), includes IE, Chrome and FF

  1. Ctrl + Shift + Del
  2. -
    1. Chrome: Select "Browsing History" and "Cache..."
    2. IE: I leave default option "Temporary Internet files and website files", "Cookies and website data", "History"
    3. FF: "Browsing and Download history", "Cache"
  3. Click "Delete"
  4. Close and reopen your browser. It should work
几度春秋 2025-01-09 13:55:07

在最新的 Google Chrome 版本 79 上,您可以使用 chrome://net-internals
并从左侧面板中选择 DNS,然后点击清除主机缓存按钮

chrome 打开网络内部页面的屏幕截图

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

Screenshot of chrome opening the net-internals page

拍不死你 2025-01-09 13:55:07

正如其他答案所示,缓存在浏览器中的持续时间可能是无限期的。这是极其危险烦人的。所以不要这样做。至少添加缓存标头。在 htaccess 中,我总是这样做,根本没有缓存:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
  # The E=nocache:1 sets the environment variable nocache to the value of one
  RewriteRule ^/?(.*) https://www.example.org/$1 [L,R=301,E=nocache:1]
</IfModule>


<IfModule mod_headers.c>
  ## Set the response header if the "nocache" environment variable is set
  ## in the RewriteRule above.
  Header always set Cache-Control "no-store, no-cache, must-revalidate" env=nocache

  ## Set Expires too ...
  Header always set Expires "Thu, 01 Jan 1970 00:00:00 GMT" env=nocache
</IfModule>

编辑:

如果您过去没有缓存 301 重定向,则必须从目标重定向回源。示例:

如果你有这个,

RewriteRule /my-source /my-target [L,R=301]

你需要把这个

# RewriteRule /my-source /my-target [L,R=301]
RewriteRule /my-target /my-source [L,R=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:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
  # The E=nocache:1 sets the environment variable nocache to the value of one
  RewriteRule ^/?(.*) https://www.example.org/$1 [L,R=301,E=nocache:1]
</IfModule>


<IfModule mod_headers.c>
  ## Set the response header if the "nocache" environment variable is set
  ## in the RewriteRule above.
  Header always set Cache-Control "no-store, no-cache, must-revalidate" env=nocache

  ## Set Expires too ...
  Header always set Expires "Thu, 01 Jan 1970 00:00:00 GMT" env=nocache
</IfModule>

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

RewriteRule /my-source /my-target [L,R=301]

You need to put this

# RewriteRule /my-source /my-target [L,R=301]
RewriteRule /my-target /my-source [L,R=301]
甜`诱少女 2025-01-09 13:55:07

为了解决本地主机地址的问题,我更改了站点运行的端口号。这适用于 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.

老旧海报 2025-01-09 13:55:07

使用隐身/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.

深海夜未眠 2025-01-09 13:55:07

移动 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.

等风来 2025-01-09 13:55:07

这是有效的答案

// Inject this script on URL-Y page load to clear URL-X 301
var url = 'URL-X';
var headers = new Headers();
headers.append('Cache-Control', 'no-cache');

fetch(url, {
  method: 'GET',
  headers: headers
});

This is the working answer

// Inject this script on URL-Y page load to clear URL-X 301
var url = 'URL-X';
var headers = new Headers();
headers.append('Cache-Control', 'no-cache');

fetch(url, {
  method: 'GET',
  headers: headers
});
等风也等你 2025-01-09 13:55:07

在你的 .htaccess 文件中尝试这个:

  <IfModule mod_expires.c>
  ExpiresActive On
  Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
  Header Set Cache-Control "max-age=0, no-store"
  Header Set Cache-Control "no-store"
  Header set Pragma "no-cache"
  </IfModule>

try this one in your .htaccess File:

  <IfModule mod_expires.c>
  ExpiresActive On
  Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
  Header Set Cache-Control "max-age=0, no-store"
  Header Set Cache-Control "no-store"
  Header set Pragma "no-cache"
  </IfModule>
无妨# 2025-01-09 13:55:07

要禁用重定向,请将此代码放入重定向站点的索引文件中

<script>
    fetch('https://www.example.com', {method: 'post'}).then(() => {})
</script>

To disable redirecting, put this code in index file of redirecting site

<script>
    fetch('https://www.example.com', {method: 'post'}).then(() => {})
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文