How to Set an Expires Header in Apache

Setting an Expires (or Cache-Control) header in Apache will help speed up your website. I’m running Apache 2.x, and define an expires header for all of the site’s static assets (images, stylesheets, and scripts).

In Apache, mod_expires is a module that allows you to set a given period to live for web pages and other objects served from web pages. The idea is to inform web browsers how often they should reload objects from the server. This will save you bandwidth and server load because clients who follow the header will reload objects less frequently.

Command to enable mod_expires:

  a2enmod expires

To set an expires header, only add the following to the “virtualHost” section of your Apache vhost configuration:

  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/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"

Alternatively, you can add it to your htaccess file in a block. Also, you should enable mod_defalate which will gzip your HTML, CSS, and Javascript

Command to enable mod_defalate:

  a2enmod deflate

And add these lines:

  # gzip html, css and js
  AddOutputFilterByType DEFLATE text/html text/css application/x-javascript application/javascript