Forum Moderators: open
I have a problem with cacheing. I need to stop browsers from cacheing my dynamic pages BUT I would like them to be able to cache images and css files.
Currently every page sends out a bunch of headers to stop caching before it does anything else.
Is there a way to do this?
Many thanks...
Nick
Here's the code I use in .htaccess on Apache:
# Set up Cache Control headers
ExpiresActive On
# Default - Set http header to expire everything 1 week from last access, set must-revalidate
ExpiresDefault A604800
Header append Cache-Control: "must-revalidate"
# Apply a customized Cache-Control header to frequently-updated files
<FilesMatch "^(intrlog¦test)\.html$">
ExpiresDefault A1
Header unset Cache-Control:
Header append Cache-Control: "no-cache, must-revalidate"
</FilesMatch>
<FilesMatch "^(calendars¦event\_sched¦4[0-9]{2}.?)\.html$">
ExpiresDefault A7200
</FilesMatch>
<FilesMatch "^index\.htm">
ExpiresDefault A7200
</FilesMatch>
<FilesMatch "^robots\.txt$">
ExpiresDefault A7200
</FilesMatch>
Check out ExpiresByType in mod_expires. For your situation, that may be easiest.
Apache mod_headers [httpd.apache.org]
Apache mod_expires [httpd.apache.org]
Apache core Files and FilesMatch [httpd.apache.org]
HTH,
Jim
Scary at first... Possibly.
But then again, it completely controls the cacheability of an entire site (since the initial "default" directives above apply to almost all files), and I have no cache issues to worry about at all... :)
I encourage you to use the above example, dig into the Apache documents, and take control!
Jim
Sure, here's a quick rundown, highlighting a few of the different "classes" of cache control I use:
# Turn on the Apache module that applies "Expires" headers to http responses
ExpiresActive On
# Declare default expiry time. All files will be declared expired one week (in seconds) after
# they were last fetched from the server. Enable transmission of the "must-revalidate" header
# to require caches to check for a new version after the expiry time has passed (otherwise it's
# optional for them to do so).
ExpiresDefault A604800
Header append Cache-Control: "must-revalidate"
# Apply a customized Cache-Control header to frequently-updated files such as my server test
# pages. In addition to setting an expirt time of one second, this code also sets the no-cache
# header, prohibiting any caching of these pages.
<FilesMatch "^(intrlog¦test)\.html$">
ExpiresDefault A1
Header unset Cache-Control:
Header append Cache-Control: "no-cache, must-revalidate"
</FilesMatch>
# This section only modifies the expiry time of the default settings established at the outset;
# it sets the expiry time of various time-sensitive pages (plus my custom 4xx-error pages) to
# two hours.
<FilesMatch "^(calendars¦event\_sched¦4[0-9]{2}.?)\.html$">
ExpiresDefault A7200
</FilesMatch>
One note: I had strange results when attempting to use the "header-append" directive to actually append new headers to existing ones. It never seemed to work correctly on the server I was testing on. So I adopted the practice of clearing the header using header-unset, and then re-specifying the entire header. As in many things, I didn't have time to figure out why it didn't work, just found a quick work-around...
Ref: Caching tutorial [mnot.net]
Ref: Cache header tester [ircache.net]
HTH,
Jim
Would like to echo Nick_W's words:
"Crikey, that looks Scary!"
That aside, what I'd like to ask after, what affect does this .htaccess insert have on these meta tags:
<meta http-equiv="PRAGMA" content="NO-CACHE" />
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
Using that .htaccess insert, can I ditch these tags? From direct experience, they seem to have no affect whatsoever. I still have outdated links and images all over the web which I need to sort out.
Further, am I to assume that this code snip goes into .htaccess at root level?