Enable Mod_Expires

Page content

How to enable the Expires Module on Apache Server Ubuntu

Mod_expires plays an important role in caching on APache server, allowing you to specify expiration time for different types of content on your website for successful caching of content.

Mod_expires controls the Expires HTTP header setting and the max-age directive of the Cache-Control HTTP header in server responses. There are two ways to set the expiration directives. One can set it relative to the time the content was modified or relative to the client access time.

For example:

ExpiresByType text/html "access plus 1 month 15 days 12 hours"
ExpiresByType image/gif "modification plus 5 hours 30 minutes"

Expires HTTP header and cache control header tell the client about the persistence and validity of the content. Once the specified period is over, the cached content is considered expired and invalid and therefore a new copy must be acquired from the source.

If ExpiresActive is set to on, it will enable the generation of expires and cache control headers. If you add these directives in .htaccess file, they will apply only to the documents inside the same directory.

The name of its source file is mod_expires.c which specifies the source containing the code for the module. The directive uses this name.

Mod_expires needs to be enabled to set cache control and expires headers. You will need it to enable browser caching of content and speed up your loading times.

First check if the module is already enabled on your server. To get a list of all te modules being loaded on your Apache server, enter the following command:

apachectl -M

In the list it outputs, if you see the following, it means the expires module is active:

Expires module (shared)

Or, you can run the following command:

sudo apachectl -M | grep expires

If the module expires is not active on your server, you can easily activate it with the following command:

sudo a2enmod expires

This will enable the expires module on Apache server Ubuntu. Now, you need to restart the Apache server to make the changes take effect.

After enabling the expires module, you can set expires headers to leverage browser caching by adding the following to your .htaccess file:

## EXPIRES HEADER CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/svg "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES HEADER CACHING ##

If you use pingdom to check your site performance, you will receive a warning if you have not added expires headers.

You can also add expires headers with the help of a caching plugin like W3TC.