Skip to content

Instantly share code, notes, and snippets.

@Fayozjon
Forked from quickstep25/htaccess-cache-config
Created November 30, 2015 10:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Fayozjon/6694ceebf37fc30b64af to your computer and use it in GitHub Desktop.
Save Fayozjon/6694ceebf37fc30b64af to your computer and use it in GitHub Desktop.
Enable and Disable Browser Caching with .htaccess
# Enable and Disable Browser Caching with .htaccess
## Enable Examples
### 1 MONTH for static assets
<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
### 1 DAY for rss feeds and robots
<filesMatch ".(xml|txt)$">
Header set Cache-Control "max-age=86400, public, must-revalidate"
</filesMatch>
### 4 HOURS for your real articles files
<filesMatch ".(html|htm)$">
Header set Cache-Control "max-age=14400, must-revalidate"
</filesMatch>
# Disable Example
<filesMatch ".(html|htm|js|css|jpg|png)$">
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, 08 Jan 1975 05:00:00 GMT"
</ifModule>
</filesMatch>
# Tips on Usage:
-- .htaccess configuration effects the directory contents in which it resides, as well as its subdirectories
-- .htaccess cannot be ready by browsers so it is secure for other operations.
-- creating the .htaccess file is best done on the server because most OS system settings will initially hide any file created that begins with a "."
@cemerson
Copy link

cemerson commented Dec 5, 2022

If a browser has already downloaded a file - like a PDF - and the above is added to the htaccess file after the fact - is there any way to force future downloads of the PDF to fetch the new version of the PDF vs the one downloaded originally? I've tried the above and other no-cache htaccess snippets but every time the old PDF is downloaded. Is the only solution (other than renaming the PDF or adding a unique query string parameter) for the end-user to clear their cache?

@Fayozjon
Copy link
Author

Fayozjon commented Dec 7, 2022

@cemerson yes just add random string on end of download url
mydownload.url?cache=RANDOMHERE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment