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

前端 未结 12 1389
情歌与酒
情歌与酒 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:56

    I got your problem...

    Although we can clear client browser cache completely but you can add some code to your application so that your recent changes reflect to client browser.

    In your <head>:

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

    Source: http://goo.gl/JojsO

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

    In my case, I change a lot an specific JS file and I need it to be in its last version in all browsers where is being used.

    I do not have a specific version number for this file, so I simply hash the current date and time (hour and minute) and pass it as the version number:

    <script src="/js/panel/app.js?v={{ substr(md5(date("Y-m-d_Hi")),10,18) }}"></script>
    

    I need it to be loaded every minute, but you can decide when it should be reloaded.

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

    This worked for me.

    look for this:

        DirectoryIndex index.php
    

    replace with this:

        DirectoryIndex something.php index.php
    

    Upload and refresh page. You will get a page error.

    just change it back to:

        DirectoryIndex index.php
    

    reupload and refresh page again.
    I checked this on all of my devices and, it worked.

    0 讨论(0)
  • 2020-12-07 15:00

    Change the name of the .CSS file Load the page and then change the file again in the original name it works for me.

    0 讨论(0)
  • 2020-12-07 15:01

    You can set "access plus 1 seconds" and that way it will refresh the next time the user enters the site. Keep the setting for one month.

    0 讨论(0)
  • 2020-12-07 15:03

    Now the following wont help you with files that are already cached, but moving forward, you can use the following to easily force a request to get something new, without changing the actual filename.

    # Rewrite all requests for JS and CSS files to files of the same name, without
    # any numbers in them. This lets the JS and CSS be force out of cache easily
    # by putting a number at the end of the filename
    # e.g. a request for static/js/site-52.js will get the file static/js/site.js instead.
    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteRule ^static/(js|css)/([a-z]+)-([0-9]+)\.(js|css)$ /site/$1/$2.$4 [R=302,NC,L]
    </IfModule>
    

    Of course, the higher up in your folder structure you do this type of approach, the more you kick things out of cache with a simple change.

    So for example, if you store the entire css and javascript of your site in one main folder

    /assets/js
    /assets/css
    /assets/...
    

    Then you can could start referencing it as "assets-XXX" in your html, and use a rule like so to kick all assets content out of cache.

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteRule ^assets-([a-z0-9]+)/(.*) /$2 [R=302,NC,L]
    </IfModule> 
    

    Note that if you do go with this, after you have it working, change the 302 to a 301, and then caching will kick in. When it's a 302 it wont cache at the browser level because it's a temporary redirect. If you do it this way, then you could bump up the expiry default time to 30 days for all assets, since you can easily kick things out of cache by simply changing the folder name in the login page.

    <IfModule mod_expires.c>
      ExpiresActive on
      ExpiresDefault A2592000
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题