22

I have a page on a site which uses random() twig, in Firefox and Chrome it is prevented from working because it gets cached as soon as the page loads.

Is there a way to turn off caching of a particular file via the Apache configs, lets call it default.html or even better just turn off caching for the script part of that file but keep caching image files?

I have tried .htaccess but this does not work.

The only way currently that allows the script to work is to turn off caching globally via PHP headers:

<?php header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache'); ?> 

But as I only need to turn off caching for an individual page, turning it off for everything seems crazy.

0

1 Answer 1

45

Figured it out, to target a specific file (in this case index.php), add this code to the bottom of .htaccess

<Files index.php> FileETag None 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" </Files> 

Alternatively to target a specific selection of files, ie. I'd like to cache images but nothing else (files that match html, htm, js, css, php will not be cached):

<filesMatch "\.(html|htm|js|css|php)$"> FileETag None 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" </filesMatch> 

To check the .htaccess was being read I entered a few lines of rubbish at the bottom, found out it wasn't being read, renamed it from htaccess to .htaccess and it worked.

Sign up to request clarification or add additional context in comments.

3 Comments

What about the images that are called on that specific page? Will they still cache?
Can we comma separate two files?
I think Expires is ignored, when Cach-Control "max-age ..." is used: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.