Advertisement
R8934

browsers 301 caching

Mar 24th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. http://stackoverflow.com/questions/9130422/how-long-do-browsers-cache-http-301s
  2.  
  3. At least two browsers - Chrome and Firefox - will cache a 301 redirect with no expiry date.
  4.  
  5. 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.
  6.  
  7. You can verify this at least in Firefox by going to about:cache and finding it under disk cache.
  8.  
  9. 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.
  10.  
  11. In all browsers, including Chrome/Firefox it is still possible to override this default behavior using headers, as described below:
  12.  
  13. Note: this answer was written in 2014 and browser behavior may change over time.
  14.  
  15. If you don't want the redirect to be cached
  16.  
  17. 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.
  18.  
  19. The browsers still honor the Cache-Control and Expires headers like with any other response, if they are specified.
  20.  
  21. 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.
  22.  
  23. 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.
  24.  
  25. 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.
  26.  
  27. If you previously issued a 301 redirect but want to un-do that
  28.  
  29. 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:
  30.  
  31. The simplest and best solution is to issue another 301 redirect back again.
  32.  
  33. 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.
  34.  
  35. Edit: some comments throw doubt upon this, see below.
  36. 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.
  37. Also prevention is better than cure - avoid a 301 redirect if you are not sure you want to permanently de-commission the old URL.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement