0

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?

  • I don't think you'd need to worry about expiring PHP files or not, seeing as (unless you have an Apache PHP caching plugin) the PHPs will always be rendered dynamically. – Fabrício Matté Jul 23 '12 at 07:10
  • OK, so do I just set `Expires` on all files? How would be best? –  Jul 23 '12 at 07:11
  • I'm not a caching expert, but people usually use the `Expires` and `Cache-Control` headers when they *don't* want caching, as all modern browsers are default to cache resources. But yes, the `mod_expires` in Alon's answer should do it. – Fabrício Matté Jul 23 '12 at 07:16

1 Answers1

2

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

Alon Adler
  • 3,984
  • 4
  • 32
  • 44