How to set up Expires header with Apache2 on Ubuntu Lucid 10.04

A very good way to reduce page load time on your website is to tell your visitors’ browser it can cache some specific files and save a copy on the disk.
This process is done by your web-server which is sending an Expires header and a max-age header during the HTTP response, e.g.:

200 OK
Cache-Control: max-age=604800
Connection: close
Date: Tue, 27 Jul 2010 22:31:03 GMT
Accept-Ranges: bytes
ETag: "2c956-376b-4696cb8b385c0"
Server: Apache/2.2.14 (Ubuntu)
Content-Length: 14187
Content-Type: image/gif
Expires: Tue, 03 Aug 2010 22:31:03 GMT
Last-Modified: Fri, 08 May 2009 20:46:23 GMT
Client-Date: Tue, 27 Jul 2010 22:31:02 GMT
Client-Peer: 127.217.30.5:80
Client-Response-Num: 1

Apache2 offers this feature through its mod_expires module. Note that this module is usually disabled by default, meaning your visitors would download all the files over again each time they change the page.

  • First, to activate mod_expires, type this in a terminal:
    sudo a2enmod expires
  • Then, we need to tell Apache2 which files can have an expire header. You can add this in your vhost configuration or your main apache2 configuration file you want it to apply to every vhost:
    ExpiresActive On
    ExpiresByType image/gif "access plus 7 days"
    ExpiresByType image/jpeg "access plus 7 days"
    ExpiresByType image/png "access plus 7 days"
    ExpiresByType image/x-icon "access plus 3 months"
    ExpiresByType image/ico "access plus 3 months"
    ExpiresByType text/css "access plus 2 days"
  • Remember to restart Apache2:
    sudo service apache2 restart
  • You can look at the headers sent by your webserver using HEAD (on a linux machine) like so:
    HEAD http://www.codealpha.net/wp-content/themes/notso_freshe/images/header.gif

2 Replies to “How to set up Expires header with Apache2 on Ubuntu Lucid 10.04”

Leave a Reply