How long do browsers cache HTTP 301s?

2018-12-31 21:49发布

问题:

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?

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

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?

回答1:

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.

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

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.

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

Note: this answer was written in 2014 and browser behavior may change over time.

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

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.

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.

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.

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 de-commissioned URL, and this should cause it re-fetch that URL again to confirm that the old redirect isn\'t still there.

    Edit: some comments throw doubt upon this, see below.

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

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



回答2:

To clear a permanent redirect, 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.

Update: Unfortunately, as of version 71 (Dec 2018) Google has removed the net-internals feature.



回答3:

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.

This worked for me in Chrome 39, but unfortunately not for localhost.



回答4:

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


回答5:

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>


回答6:

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.com 301 to b.com

  2. delete a.com \'s 301

  3. add b.com 301 to a.com

Then it works.



回答7:

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


回答8:

For testing purposes (to avoid cached redirects), people can open NEW PRIVATE WINDOW: click CTRL+SHIFT+N [if you use Mozilla, use P]



回答9:

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.



回答10:

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.



回答11:

As the other answers show. Caching may be indefinetly in browser. This is extremely dangerous. So don\'t do it. At least add cache headers. In htaccess I always do it this way with now 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>