Forum Moderators: open

Message Too Old, No Replies

Cache Issues with Dynamic Pages

How to get browsers to cache some things but not others?

         

Nick_W

3:16 pm on Jun 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

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

jdMorgan

5:53 pm on Jun 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

Nick_W

6:08 am on Jun 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Crikey, that looks Scary!

Thanks Jim!

Nick

jdMorgan

9:52 pm on Jun 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick,

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

Nick_W

11:02 am on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll do that jim!

Can I get you to give me an explanation of what your code does above? I don't mean step by step, but if you could sum it up like "it does this that and this" it would be really, really helpful.

Thanks ;)

Nick

jdMorgan

4:44 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick_w,

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

jackson

11:40 am on Jun 19, 2003 (gmt 0)

10+ Year Member



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?