how to add expires headers to your images


During a meeting at work today, we looked at my web site (as an example) to see if there are ways the performance could be improved. I was surprised to see how many things could be done to make it a big faster.

One of the most trivial changes involved adding an Expires: header which specifies a time far enough in the future so that clients (browsers) won’t try to re-fetch images that haven’t changed.

Since I’m running Apache 1.3, I dug up the old documentation on mod_expires and made a few simple additions to httpd.conf:

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000

That asks Apache to set an Expires header that’s roughly one month from the moment at which the browser requested the image. Here’s a quick test to show that it worked:

root@litterbox:~/w/i# telnet localhost 80
Connected to localhost.
Escape character is '^]'.
HEAD /i/mini-softie.jpg HTTP/1.0

HTTP/1.1 200 OK
Date: Mon, 23 Jul 2007 22:03:35 GMT
Server: Apache/1.3.34 (Debian) mod_gzip/ PHP/4.4.4-8
Cache-Control: max-age=2592000
Expires: Wed, 22 Aug 2007 22:03:35 GMT
Last-Modified: Tue, 29 May 2007 15:51:04 GMT
ETag: "95c67-5995-465c4be8"
Accept-Ranges: bytes
Content-Length: 22933
Connection: close
Content-Type: image/jpeg

Excellent. August 22nd is about a month from now.

In theory I could go quite a bit farther into the future since I almost never replace images on my site.


About Author

Leave A Reply