How to prevent http file caching in Apache httpd (MAMP)

Apache.HtaccessHttp HeadersMamp

Apache Problem Overview


I am developing a single page Javascript application in MAMP. My JavaScript and HTML template files are getting cached between requests.

Is there a simple way to indicate in MAMP that I want to prevent http file caching? Possibly with a .htaccess file? Where do I place the .htaccess or modify the virtual host for MAMP on Mac?

Apache Solutions


Solution 1 - Apache

Tried this? Should work in both .htaccess, httpd.conf and in a VirtualHost (usually placed in httpd-vhosts.conf if you have included it from your httpd.conf)

<filesMatch "\.(html|htm|js|css)$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

> 100% Prevent Files from being cached > > This is similar to how google ads employ the header Cache-Control: private, x-gzip-ok="" > to prevent caching of ads by proxies and clients.

From http://www.askapache.com/htaccess/using-http-headers-with-htaccess.html

And optionally add the extension for the template files you are retrieving if you are using an extension other than .html for those.

Solution 2 - Apache

Based on the example here: http://drupal.org/node/550488

The following will probably work in .htaccess

 <IfModule mod_expires.c>
   # Enable expirations.
   ExpiresActive On

   # Cache all files for 2 weeks after access (A).
   ExpiresDefault A1209600

  <FilesMatch (\.js|\.html)$>
     ExpiresActive Off
  </FilesMatch>
 </IfModule>

Solution 3 - Apache

I had the same issue, but I found a good solution here: https://stackoverflow.com/questions/19073270/stop-caching-for-php-5-5-3-in-mamp

Basically find the php.ini file and comment out the OPCache lines. I hope this alternative answer helps others else out as well.

Solution 4 - Apache

Without mod_expires it will be harder to set expiration headers on your files. For anything generated you can certainly set some default headers on the answer, doing the job of mod_expires like that:

<?php header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600)); ?>

(taken from: Stack Overflow answer from @brianegge, where the mod_expires solution is also explained)

Now this won't work for static files, like your javascript files. As for static files there is only apache (without any expiration module) between the browser and the source file. To prevent caching of javascript files, which is done on your browser, you can use a random token at the end of the js url, something like ?rd=45642111, so the url looks like:

<script type="texte/javascript" src="my/url/myjs.js?rd=4221159546">

If this url on the page is generated by a PHP file you can simply add the random part with PHP. This way of randomizing url by simply appending random query string parameters is the base thing upôn no-cache setting of ajax jQuery request for example. The browser will never consider 2 url having different query strings to be the same, and will never use the cached version.

EDIT

Note that you should alos test mod_headers. If you have mod_headers you can maybe set the Expires headers directly with the Header keyword.

Solution 5 - Apache

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondmckView Question on Stackoverflow
Solution 1 - ApacheCharlie RudenstålView Answer on Stackoverflow
Solution 2 - ApacheFrank FarmerView Answer on Stackoverflow
Solution 3 - ApacheacaritoView Answer on Stackoverflow
Solution 4 - ApacheregileroView Answer on Stackoverflow
Solution 5 - ApacheBipin BahugunaView Answer on Stackoverflow