Add expires headers to your apache vhost

In an effort to become more organised in what I need to do when launching a new website, I created a checklist of pre and post launch tasks. I recently completed a site for some family friends (shameless plug, check it out) and it proved to be invaluable.

One thing I almost always forget to do is to set expires headers for static assets. To add expires headers to your apache vhost for static assets, simply add the following between your <directory></directory> tags:

<ifModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif "access plus 1 months"
    ExpiresByType image/jpg "access plus 1 months"
    ExpiresByType image/jpeg "access plus 1 months"
    ExpiresByType image/png "access plus 1 months"
    ExpiresByType image/svg+xml "access plus 1 months"
    ExpiresByType image/vnd.microsoft.icon "access plus 1 months"
    ExpiresByType image/x-icon "access plus 1 months"
    ExpiresByType image/ico "access plus 1 months"
    ExpiresByType application/javascript "now plus 1 months"
    ExpiresByType application/x-javascript "now plus 1 months"
    ExpiresByType text/javascript "now plus 1 months"
    ExpiresByType text/css "now plus 1 months"
    ExpiresDefault "access plus 1 days"
</ifModule>

This will set the expires headers for most image formats, as well as css and JS. I've added a check to ensure you have mod_expires first so we don't have a 'tango down' moment if the module isn't enabled.

Enable mod expires

If you haven't already got mod expires enabled, just run the following command:

sudo a2enmod expires

That should do the trick. Give apache a restart and you're ready to rock. Feel free to ask questions or share your thoughts below.