How long do browsers cache HTTP 301s?

前端 未结 15 2286
囚心锁ツ
囚心锁ツ 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:11

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

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

    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.

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

    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.

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

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

    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.

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