Htaccess - How to force client browser to clear cache?

For my site, I have the following htaccess rules:

# BEGIN Gzip <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript </IfModule> # END Gzip # BEGIN EXPIRES <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 10 days" ExpiresByType text/css "access plus 1 month" ExpiresByType text/plain "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType application/x-javascript "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" ExpiresByType application/x-icon "access plus 1 year" </IfModule> # END EXPIRES 

I just updated my site and it looked all sluggish until I cleared my cache. How to force the client browser to clear the cache after the update so that the user can see the changes?

+45
caching gzip .htaccess
Dec 01 '11 at 20:09
source share
10 answers

You can force browsers to cache something, but

You cannot force browsers to clear the cache.

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

+81
Dec 04 2018-11-11T00:
source share

As other answers said, changing the URL is a good way to iterate over the cache, however most of the work goes through a large site, changes all the URLs and also moves the files.

A similar method is to simply add the version parameter to the URL string, which is a random string / number or version number, and only target the modified files.

For example, if you change your CSS site and it looks awkward until you refresh the force, just add ?ver=1.1 to the CSS import at the beginning of the file. This is a different file for the browser, but you need to change the import, not the actual location or file name.

eg:

<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" />

Great for javascript files.

+39
Aug 26 '13 at 8:21
source share

I'm having your problem ...

Although we can completely clear the client’s browser cache, you can add code to your application so that your latest changes are reflected in the client’s browser.

In <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

+12
Dec 06 2018-11-12T00:
source share

You cannot force browsers to clear the cache.

Your .html file seems to reload before it expires in 10 days. You need to update your .html file and move all your files to a new folder, for example version-2/ , or add a version identifier for each file, for example mypicture-2.jpg . You then link to these new files in your .html file, and the browser will download them again because the location has changed.

+4
Dec 08 '11 at 19:27
source share

You can say that the browser never caches your site by pasting the 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 can use a tool to minimize and obfuscate scripts that must generate an arbitrary file name every time. This will force the browser to restart them from the server.

Hope this helps.

+2
May 19 '14 at 1:43
source share

You can set "access plus 1 second", and thus it will be updated the next time the user logs on to the site. Save the setting for one month.

+1
Sep 10 '13 at 10:59 on
source share

The easiest way is to add a file to the request. eg,

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

version by date.

+1
Oct 30 '14 at 23:34
source share

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

0
Sep 03 '15 at 12:41
source share

In my case, I am changing a lot of specific JS file, and I need it to be in the latest version in all browsers where it is used.

I don’t have a specific version number for this file, so I just 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("Ym-d_Hi")),10,18) }}"></script> 

I need it to load every minute, but you can decide when it needs to be rebooted.

0
Jun 02 '17 at 19:27
source share

Now the following will not help you with files that are already cached, but moving forward, you can use the following to simplify the request for something new without changing the file name.

 # 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 # eg 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)/([az]+)-([0-9]+)\.(js|css)$ /site/$1/$2.$4 [R=302,NC,L] </IfModule> 

Of course, the higher you take this approach in your folder structure, the more you throw things out of the cache with a simple change.

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

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

Then you can start referring to it as "assets-XXX" in your html and use such a rule to knock out all the contents of the resources from the cache.

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

Note that if you do this, after you earn it, change the value of 302 to 301, and then start caching. When it is 302, it will not cache at the browser level, because it is a temporary redirect. If you do this, then you can increase the expiration time to 30 days for all assets, since you can easily throw things out of the cache by simply changing the name of the folder on the login page.

 <IfModule mod_expires.c> ExpiresActive on ExpiresDefault A2592000 </IfModule> 
0
Jul 18 '17 at 12:50
source share



All Articles