Forum Moderators: phranque
# 1 YEAR
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
# 2 HOURS
Header set Cache-Control "max-age=7200, must-revalidate"
# CACHED FOREVER
# MOD_REWRITE TO RENAME EVERY CHANGE
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
Yeah... Around Sept. 27, I started getting e-mails from people saying they weren't seeing any new posts. I removed the code, sent out an e-mail asking people to clear their cache, and hoped that would work.
The thing is, I'm still getting e-mails from people who don't sign up to my newsletter and thus didn't get the "clear your cache" message. So I'm wondering if there's any way to force a cache-clear for my users?
Thanks in advance to anybody who might be able to help me! At the very least, I now know not to mess with my .htaccess unless I know what I'm doing!
Here's a working example of a "mostly by filename" implementation:
# Set up Expires and Cache Control headers
ExpiresActive On
#
# Default - Set Cache-Control header to expire everything 3 days from last access
ExpiresDefault A259200
Header set Cache-Control: "must-revalidate"
#
# Uncacheable files (custom help, test, and error documents)
<FilesMatch "(search-help¦test[0-9]?¦401¦403¦404¦410¦500)\.html$">
ExpiresDefault A0
ErrorHeader set Cache-Control: "no-store"
</FilesMatch>
#
# Password-protected pages -- not cacheable in public network, only in browsers, expire after 2 hours
<FilesMatch "^mod-schedule\.html$">
ExpiresDefault A14400
Header set Cache-Control: "private, must-revalidate"
</FilesMatch>
#
# Frequently-updated files, expire after 1 hour
<FilesMatch "^(contest-results¦news)\.html$">
ExpiresDefault A3600
Header set Cache-Control: "no-cache, must-revalidate"
</FilesMatch>
#
# Fairly-frequently updates -- expire after 4 hours
<FilesMatch "^(index¦calendar[0-9]{4})\.html$">
ExpiresDefault A28800
Header set Cache-Control: "no-cache, must-revalidate"
</FilesMatch>
#
# Cache images for up to two weeks -- no forced revalidation
<FilesMatch "\.(gif¦jpe?g¦png¦bmp¦ico)$">
ExpiresDefault A1209600
</FilesMatch>
Note also that "no-cache" doesn't mean the resource is not cacheable. This is due to some rather large errors made in the first generation of HTTP caches. In fact, I can't tell you the exact interpretation of it, just that it forces revalidation as needed. "No store" is the only cache-control directive that makes a resource fully-uncacheable.
The first cache-control section above sets the "default" for the whole site -- all kinds of objects and resources on the site -- everything. That default is then overridden for more-specific groups of objects/resources by the later directives.
The default setting in the first section is to allow the client some leeway in checking for updates, even after the object is expired. I only force strict compliance for the important objects in most of the later more-specific sections. If you get too strict with unimportant objects, then your server will suffer because of increased requests, so it is a trade-off between 'actually-required freshness' and server load.
I should also note that there is usually a very large benefit to short-term caching (e.g. hours), but almost no benefit at all in increasing the expires time beyond a few days except for images or media files. Setting the cache time to a month is "almost ridiculous" in almost all cases, since there is almost no decrease in server load by doing so; the cache entries in most users' browsers will have been over-written by more-recently-fetched objects (from other sites) long before then.
The effectiveness of caching in saving server bandwidth rises very fast as the cache time is increased from zero to a few minutes, continues to rise at a moderate rate for up to several hours, and then begins to rise less and less quickly, until the difference in benefit between a two-week cache time and a one-month cache time is practically zero. Of course, this is a generalization on the exact times, and will vary from site to site, but the shape of that curve always looks the same -- it is what is called a "knee curve."
Important Note: Replace all broken pipe "¦" characters in the code above with solid pipe characters before use; Posting on this forum modifies the pipe characters.
Proceed carefully, and think carefully about how long you actually need things to be cached for, and where you want them cached (e.g. in public-network caches, private-client caches, or both.) Check your work carefully using a good server headers checker like the "Live HTTP Headers" add-on for Firefox/Mozilla browsers to make sure the cache-control and expires headers for each object/page/resource are what you wanted.
Jim