155

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?

0

5 Answers 5

344
+50

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.

7
  • 3
    Thank you, this works very well in a .htaccess file. I can see the cache control options when I inspect the headers and my files are showing up with http 200 instead of 304 between requests, just what I needed.
    – dmck
    Jul 30, 2012 at 22:26
  • 2
    Don't forget to add the LoadModule command. LoadModule headers_module lib/modules/mod_headers.so
    – Spenhouet
    Sep 22, 2014 at 14:24
  • Can I apply those header to all file types, instead of defining extensions one by one? Jan 13, 2017 at 3:17
  • 1
    @ThariqNugrohotomo Yes it's possible! It's a regular expression, you can try <filesMatch "\.+"> or <filesMatch ^> Jan 25, 2017 at 11:28
  • I'm not using MAMP but Apache on a web server. I had to also enable headers and expires modules on my Apache installation by typing "sudo a2enmod headers" and "sudo a2enmod expires", and then sudo service apache2 restart. The LoadModule and IfModule commands/tags weren't necessary -- just omit the bracketed IfModule tag parts above but still enter the Header settings between them. –
    – andruo11
    Jun 10, 2018 at 20:13
5

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>
3
  • Unfortunately this does not work when I put a .htaccess file in my project directory. I'm not sure if mod_expires is active.
    – dmck
    Jul 24, 2012 at 0:37
  • 5
    @dmck: remove the <IfModule mod_expires.c> and </IfModule> section .. if mod_expires isn't enabled, you'll get an error instead of those directives quietly being ignored.
    – Stennie
    Jul 28, 2012 at 13:15
  • This simply disables mod_expires for these files. It's possible that the server (or another module) is setting these headers. A general (any server) solution would need to make sure these headers are overriden (either with mod_expires again, or mod_headers - which would also override mod_expires).
    – MrWhite
    Mar 23, 2023 at 23:23
3

I had the same issue, but I found a good solution here: 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.

1
  • 4
    that is about PHP caching on the server side, a.k.a OPCache. The question is about files being cached by the browser that are requested over HTTP.
    – Flion
    Jan 30, 2016 at 17:56
2

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.

2
  • Thanks, unfortunately I am not using any PHP files. I will look into mod_headers and installing mod_expires.
    – dmck
    Jul 25, 2012 at 19:28
  • What about for preventing caching of CSS files? Jan 6, 2019 at 18:14
1
<FilesMatch "\.(js|css)$">
  ExpiresActive On
  ExpiresDefault A1
  Header append Cache-Control must-revalidate
</FilesMatch>
2
  • That's not a good quality answer. Try to explain in detail what are the steps for resolving the issue reported on the OP.
    – leopal
    Sep 3, 2019 at 6:33
  • Why A1 and not A0 or even M0, since these are static assets?
    – MrWhite
    Mar 23, 2023 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.