Welcome to WebmasterWorld Guest from 34.238.189.171

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Force a re-cache via htaccess?

readers aren't seeing new updates

     
10:50 pm on Oct 1, 2009 (gmt 0)

New User

10+ Year Member

joined:Mar 6, 2009
posts:6
votes: 0


I apologize for the vague title, but I'm not sure what remedy it is I need. Long story short, on Sept. 24 I placed the following code in my .htaccess without fully understanding it.

# 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!

12:58 am on Oct 2, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


You can control caches by filetype or by filename. Before choosing, think about this carefully, because you need to use the method most appropriate to your site.

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 the "must-revalidate" cache-control setting -- This forces the client (e.g. browser) to check with your server using an "If-Modified-Since" header; If the resource has been modified since the user last cached it, then your server will send the updated resource. If not, the server will respond with "304-Not Modified" only, and the resource will be displayed from the user's cached copy.

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

2:50 am on Oct 2, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


By the way, note that I am using the Expires-After form of Expires (see mod_expires), so that the user's cache expires at *some fixed time after* the resource was last fetched and cached. This frees you from the nightmare of having to specify and maintain fixed dates and times.

Jim

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members