Question

To speed up my website I have read about the Expires header, how to implement it in Apache, and how to only do so for certain file types. However I want to set a one month expiry on all files except .php files (webpages).

So how do I do this with .htaccess? I have a PHP script that uses filemtime to change the filename of CSS, JS files when they are edited, so that they are re-downloaded (eg if a file changes at xxxxxxx in Unix time, it changes the filename to /resource.css?recache=xxxxxxxx when it is requested). So that's taken care of. As images etc. don't change often, I want all of them to be cached for a month.

Also, what is the browser support for the Expires header?

Was it helpful?

Solution

Use the mod_expires in your Apache configuration files. For example:

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0

# Set up caching on media files for 1 month
<filesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|gif|jpg|jpeg|png|swf)$">
ExpiresDefault A2592000
Header append Cache-Control "public"
</filesMatch>

You can read more here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top