How long do browsers cache HTTP 301s?

250,085

Solution 1

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.

Solution 2

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.

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

Solution 4

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

Solution 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>
Share:
250,085
Liam
Author by

Liam

With over 15 years experience as a Web Developer, I have gained in-depth knowledge of a wide range of web technologies. I have been responsible for the architecture of several key pieces of software for a diverse portfolio of clients, in the Photography, Education, Tourism, and Retail industries. I mostly work with PHP and JavaScript.

Updated on July 08, 2022

Comments

  • Liam
    Liam almost 2 years

    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?
  • rob
    rob over 11 years
    Although technically correct, your answer does not answer the users question and hence does not answer the question I came here for. When do existing, un-cache header-ed 301s already in the browser expire for the main browsers ?
  • Francisco Presencia
    Francisco Presencia almost 11 years
    If anyone is still interested, this link instructions can toggle the cache for 301.
  • an phu
    an phu almost 11 years
    The link is for FF and it didn't work for me. Installed web developer extension 1.2.5 and using FF 23.0.1
  • Kevin Christopher Henry
    Kevin Christopher Henry over 10 years
    Excellent answer. In my opinion, though, since the HTTP standard specifically includes mention of changing the cache time on 301 redirects, these "impermanent" permanent redirects are well within the spirit of the standard.
  • Kevin Christopher Henry
    Kevin Christopher Henry over 10 years
    Also, do you have any references that show that browsers handle circular permanent redirects by re-fetching the original URL?
  • Yuriy Kolodovskyy
    Yuriy Kolodovskyy about 10 years
    301 redirect back don`t work, browser still cache old 301 redirect and i see infinite loop
  • thomasrutter
    thomasrutter about 10 years
    Yuriy which browser? How did you test?
  • Yuriy Kolodovskyy
    Yuriy Kolodovskyy about 10 years
    it was chrome and firefox
  • Yuriy Kolodovskyy
    Yuriy Kolodovskyy about 10 years
    how I did test: some time ago I did make 301 redirect for http://www.SOMEHOST.com to https://www.SOMEHOST.com. But now http://www.SOMEHOST.com must be primary host for site. So, redirect from https to http removed. As you show I did make redirect 301 from https://www.SOMEHOST.com to http://www.SOMEHOST.com, but see loop. Browser did not re-fetching...
  • Dennis Flagg
    Dennis Flagg about 10 years
    the question was not answered. The question was, how long will the redirection be cached for if no expiration date was specified
  • alfonx
    alfonx about 9 years
    This has been downrated, probably because the primary promise of the "private window" is not to WRITE to caches, but can still READ/REUSE them. BUT for me on Firefox 37.0.1 (Linux) this worked and was very quick and useful. The private window is reflecting the current/uncached settings of the web-server, whereas the normal browser tabs use a cached 301 redirect.
  • Zdenek
    Zdenek about 9 years
    alfonx: The private window may not reuse the cache simply because the server owner could use the elements in a cookie fashion revealing that user's previous identity. Although I must concede that cache reusing is probably safe against a porn-hating wife.
  • Sean
    Sean about 9 years
    @thomasrutter - In your "simplest and best solution", does it matter what domain and/or path issues the 301 redirect? Say I have legacysite.com that 301 redirects to newsite.com, and now I want to "break" that 301 redirect. Does the new 301 redirect have to come from the exact target of the original 301 redirect? Or can it come through a special workaround flow involving legacysite.com? Or perhaps it has to be issued by newsite.com but the path doesn't matter?
  • jave.web
    jave.web about 8 years
    Sometimes domain providers have service that (301) redirects domain to another www address - if it was a Facebook page (or any other not-you-controlled-page) - you can only pray they've set some expiration to that redirect... otherwise you are F...........................................................‌​....................‌​....................‌​....................‌​...rozen in time :)
  • Arth
    Arth about 8 years
    It's worth noting that your redirect-back-again strategy only applies when you want ALL the traffic to the original 301 target to be sent back to the original page. It is highly possible that this isn't the case when the original target combined two separate traffic streams.
  • B T
    B T almost 8 years
    But then you still have b.com's 301 lying around : ( - a dirty fix
  • nemec
    nemec almost 8 years
    Can you clear a redirect by issuing another 301 from a different page? e.g. (a.com 301 -> b.com) (delete a.com's 301) (add a.com/abcdefg 301 -> a.com) and force the client to view a.com/abcdefg somehow?
  • crackmigg
    crackmigg almost 8 years
    This works and even after re-enabling caching the redirect is gone. THX!
  • jeffmcneill
    jeffmcneill over 7 years
    This doesn't work if you already have a cached 301. Private will indeed use the redirect that is cached.
  • pwagner
    pwagner over 7 years
    As of Chrome version 54, this is unfortunately not working for me.
  • pwagner
    pwagner over 7 years
    I should add that I have an entry in my /etc/hosts pointing the domain to 127.0.0.1 - this is most likely relevant.
  • pwagner
    pwagner over 7 years
    It looks like this is not working for domains pointed to 127.0.0.1 via the local hosts file. Is there any other option for this case?
  • Vincent Poirier
    Vincent Poirier over 7 years
    I confirm that redirecting back (with a PHP redirection in my case) works perfectly on Google Chrome as long as (obviously) you removed the initial 301 redirect.
  • Munhitsu
    Munhitsu over 7 years
    Thanks it works! Tested on IE11, Firefox 52, Safari 10, Chrome 57
  • Munhitsu
    Munhitsu over 7 years
    I can confirm that redirect back works perfectly fine. Browsers seeing redirect loop invalidate the cache entries. Tested on IE11, Firefox 52, Safari 10, Chrome 57.
  • rosell.dk
    rosell.dk about 7 years
    I had a situation where wanted to use both a.com and b.com. So b.com 301 a.com was not an option. Our solution was to move to HTTPS - we had no redirects on HTTPS
  • MattBoothDev
    MattBoothDev almost 7 years
    Works in Version 60.0.3112.101 (Official Build) (64-bit)
  • Preston Crawford
    Preston Crawford almost 7 years
    This should be the accepted answer. I was having trouble with this and this was the only thing that worked in Version 61.0.3163.91.
  • McGuireV10
    McGuireV10 over 6 years
    @PrestonCrawford I tried tagging the OP in case he'd accept as the answer, but for whatever reason it didn't work...
  • Dennis98
    Dennis98 over 6 years
    Well, it works, but the thing is: this clears the whole HTTP Cache! Not really the best solution, if I only want to clear it for this one page, or domain.
  • Dennis98
    Dennis98 over 6 years
    Doesn't work if the redirect, unintendedly, points to another port, like from localhost:8000 to localhost (port 80). I also cleared the whole site/application data from both localhost and localhost:8000, but neither did this help.
  • McGuireV10
    McGuireV10 over 6 years
    On second thought, I didn't really answer the real question, "How long do browsers cache a 301," and my answer wouldn't help anybody who redirected a public-facing site where you probably need some way to permanently undo a 301 without knowing how many browsers in the wild have cached the redirect -- other answers partially address that scenario. My answer is really only useful to developers or intranet scenarios where you can communicate with all impacted users.
  • esjay
    esjay about 6 years
    I agree this is the best method I have found to unscrew it.
  • Oliver Schimmer
    Oliver Schimmer about 6 years
    You should also make sure you are not on the page in question, because some browsers don't clear cached items from open pages.
  • Thum Choon Tat
    Thum Choon Tat almost 6 years
    Works in Chrome version 68.0.3440.106 (Official Build)
  • yunzen
    yunzen over 5 years
    The redirect back solution does not work for Brave Browser: V 0.25.2; Windows 10; The result is a 'too many redirects' error
  • t-jam
    t-jam over 5 years
    chrome://net-internals has been gutted in Chrome 71. The drop-down / Tools section is gone. There is a DNS > Host resolver cache > Clear host cache button, but this doesn't work to remove cached 301s.
  • Bemmu
    Bemmu over 5 years
    In Chrome 71, chrome://settings/clearBrowserData and from there only clearing "cached images and files" cleared the redirect.
  • Petr Flaks
    Petr Flaks over 5 years
    Version 71.0.3578.98 (Official Build) (64-bit) for Linux still has this page, and it works. Thank you!
  • cr0ybot
    cr0ybot over 5 years
    Thank you @Bemmu! Clearing cached images & files worked!
  • Alexey Rytikov
    Alexey Rytikov over 5 years
    unlike other answers, this method is suitable for solving other people's problem without opening the developer console! thank you
  • calvin
    calvin over 5 years
    fetch('URL', {method: 'POST'}) oughta do the trick in the same way. Thanks! This saved me some headache!
  • user36388
    user36388 about 5 years
    I can't believe that this is the only way that worked for me (I tried all the other methods first - even the console fetch() failed due to a cross-site security policy).
  • sgon00
    sgon00 almost 5 years
    Thank you very much! Simple solution and Worked! This way should work in the future too.
  • Peter Wooster
    Peter Wooster over 4 years
    This solution works on Chrome as of Nov 16, 2019 version 78.0.3904.97. The other solutions are no longer available. After you get it working you can close the developer tools and it will continue to work properly.
  • webaholik
    webaholik over 4 years
    Working in Chrome 79!
  • Aysennoussi
    Aysennoussi over 4 years
    Accepted answer.
  • Steve Withington
    Steve Withington about 4 years
    @Yuck, technically speaking, this is not the best answer to the OP question. It answers how to clear the cache, not "how long do browsers cache 301s"
  • McGuireV10
    McGuireV10 about 4 years
    @SteveWithington which is exactly what I noted almost 3 years ago.
  • jpswade
    jpswade about 4 years
    This is similar to the post form above, only it's less effort.
  • Dark Hippo
    Dark Hippo almost 4 years
    Really want to upvote, but also like the fact that the answer to a question about 301 redirects has 301 upvotes...
  • thomasrutter
    thomasrutter almost 4 years
    Looks like someone else Found this answer now. It's on 302.
  • Tõnu Samuel
    Tõnu Samuel over 3 years
    Why 301 if you can do 302 from b?
  • Keno
    Keno over 3 years
    Thank goodness I found this before having to clear my browser data. Once I got it working, I could close the dev tools and continue normally.
  • Biegacz
    Biegacz over 3 years
    Thank you, the only answer that worked for me.
  • Liam
    Liam about 3 years
    This would tell browser that visit your site to not cache anything, and it is useful to know how to do this. The problem is that if you have mistakenly issued 301 redirects away from your domain, perhaps due to a typo, those browsers have cached those redirects, and they will not make requests to your site again, and therefore will not get these new headers that you have suggested.
  • Mark Rogers
    Mark Rogers over 2 years
    This isn't working for me (testing on Android's Chrome). Use case: previously had OpenWRT router, which 301 redirects to /cgi-bin/luci/. I've had to switch to a different router, and now I can only access its GUI in a private tab because of the cached redirect.
  • hughjdavey
    hughjdavey over 2 years
    This also worked on Firefox for me