Apache prevent web browsers from caching

In order to get the latest effections, Front-end develop need to refresh the web page frequently.

there some solution for fix page cache problem:

Ensure the request url is unique.

by adding random seed to URL. such as

<script src="http://xx.com/foo/path.js?v=${version}"></script>

But this case need some server programe to generic the html page resouces references dynamic.

The other one and the more simple way is prevent Web browsers by customize HTTP Headers.

Setting a short cache time

By asking the Web browser to only cache the file for a very short length of time, you can usually avoid the problem.

We support the Apache Web server “mod_expires” feature. To use this to set the cache time to just one second, add these lines to a .htaccess file:

ExpiresActive On
ExpiresDefault A1
Header append Cache-Control must-revalidate

That’s all it takes. The next time the visitor views the file, more than a second will have passed, so the browser won’t use the outdated cached copy.

Controlling which files are affected

Disabling browser caching will slow down your site and increase the amount of bandwidth your site uses, because repeat visitors will always connect to your site to re-download files they would otherwise cache.

To minimize that impact, you might want to prevent caching of files that change often (such as HTML files), while allowing normal caching of files that don’t (such as JPEG files).

To do this, include the .htaccess lines in a <FilesMatch> directive. For example, these lines will prevent caching of filenames ending in “.htm” or “.html”, while allowing normal caching of JPEG files:

<FilesMatch "\.(htm|html|js|css)$">
  ExpiresActive On
  ExpiresDefault A1
  Header append Cache-Control must-revalidate

(An alternate way of doing this is to use the Apache “ExpiresByType text/html” command, but that doesn’t allow you to add the “Cache-Control: must-revalidate” header for only those pages.)

For more info about Apache headers see also: Manipulating HTTP Headers with htaccess.