htaccess - How to force the client's browser to clear the cache?

前端 未结 12 1372
情歌与酒
情歌与酒 2020-12-07 14:23

For my site I have the following htaccess rules:

# BEGIN Gzip

AddOutputFilterByType DEFLATE text/text text/html text/plain tex         


        
相关标签:
12条回答
  • 2020-12-07 14:38

    You can force browsers to cache something, but

    You can't force browsers to clear their cache.

    Thus the only (AMAIK) way is to use a new URL for your resources. Something like versioning.

    0 讨论(0)
  • 2020-12-07 14:39

    As other answers have said, changing the URL is a good cache busting technique, however it is alot of work to go through a bigger site, change all the URLs and also move the files.

    A similar technique is to just add a version parameter to the URL string which is either a random string / number or a version number, and target the changed files only.

    For instance if you change your sites CSS and it looks wonky until you do a force refresh, simply add ?ver=1.1 to the CSS import at the head of the file. This to the browser is a different file, but you only need to change the import, not the actual location or name of the file.

    e.g:

    <link href="assets/css/style.css" rel="stylesheet" type="text/css" />

    becomes

    <link href="assets/css/style.css?ver=1.1" rel="stylesheet" type="text/css" />

    Works great for javascript files also.

    0 讨论(0)
  • 2020-12-07 14:50

    The most straight forward is to add filetime to the request. eg

    myfile.txt?2014-10-30-13:12:33

    versioning by date.

    0 讨论(0)
  • 2020-12-07 14:53

    You can not force the browsers to clear the cache.

    Your .html file seems to be re-loaded sooner as it expires after 10 days. What you have to do is to update your .html file and move all your files to a new folder such as version-2/ or append a version identifier to each file such as mypicture-2.jpg. Then you reference these new files in your .html file and the browser will load them again because the location changed.

    0 讨论(0)
  • 2020-12-07 14:54

    You can tell the browser never cache your site by pasting following code in the header

    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    

    And to prevent js, css cache, you could use tool to minify and obfuscate the scripts which should generate a random file name every time. That would force the browser to reload them from server too.

    Hopefully, that helps.

    0 讨论(0)
  • 2020-12-07 14:55

    Use the mod rewrite with R=301 - where you use a incremental version number:

    To achieve > css/ver/file.css => css/file.css?v=ver

    RewriteRule ^css/([0-9]+)/file.css$ css/file.css?v=$1 [R=301,L,QSA]

    so example, css/10/file.css => css/file.css?v=10

    Same can be applied to js/ files. Increment ver to force update, 301 forces re-cache

    I have tested this across Chrome, Firefox, Opera etc

    PS: the ?v=ver is just for readability, this does not cause the refresh

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