Prevent caching of static content using Apache config

Sometimes you have static content like a Flash SWF file which you want to prevent browsers from caching. Normally allowing clients to cache this type of content is useful however if you update it not all browsers will immediately fetch the new version.

To force a client to grab the new copy you can use the HTTP headers Cache-Control and Expires. It is easy to do this in a dynamic page coded with PHP or similar. But how can you do this for static content?

Luckily the Apache server has an extension module called mod_expires which allows us to do this.

Basically mod_expires defines three directives:

This is required to switch on or off generation of the Expires and Cache-Control headers.
Allows you to specify a expiration rule for a specific MIME type.
Allows you to set up a default expiration rule for all content within it’s scope.

Here’s a small example

# Switch on the Expires and Cache-Control
# headers
ExpiresActive On

# Set a default rule: headers will report
# expiration date as 4 weeks from when
# the page is accessed
ExpiresDefault "access plus 4 weeks"

# Flash content is updated often so
# this sets immediate expiration.
ExpiresByType application/x-shockwave-flash "access plus 0 minutes"