How long do browsers cache HTTP 301s?

前端 未结 15 2288
囚心锁ツ
囚心锁ツ 2020-11-22 14:43

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.

相关标签:
15条回答
  • 2020-11-22 15:24

    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.

    0 讨论(0)
  • 2020-11-22 15:24

    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.

    0 讨论(0)
  • 2020-11-22 15:26

    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
    0 讨论(0)
  • 2020-11-22 15:29

    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

    0 讨论(0)
  • 2020-11-22 15:29

    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.

    0 讨论(0)
  • 2020-11-22 15:31

    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.

    0 讨论(0)
提交回复
热议问题