Categories: Apache

How to enable mod_expires in Apache?

When you visit a website, it’s static content is cached in your Browser. So, when you visit the website next time, the content is loaded from the browser cache and the website will be loaded faster. The cache remains in the browser till the browser history is cleared.

We can set the Cache-Control for the static files on the server through which the browser knows till what interval the content will be cached and when to fetch new content from the source. This is achieved using mod_expires module of Apache.

The following sections will explain the basics of setting up the Apache Module mod_expires in your .htaccess.

Basic code for setting expire dates for cache

In order to add browser caching to your website, you will need to set the date for when the cache expires. This cache code is placed in the .htaccess found in your public_html folder. You will need to edit your .htaccess file. Add the following code to the file and save it.

ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"

Now your site will set the time each for each resource that was added to the .htaccess to expire. In the previous code example, the jpg, jpeg, gif, png will expire in a year and pdf, javascript and flash files will expire in a month. More explanation o these settings will come later in this article. Next we will look at using the <ifmodule> for adding Cache-Control with mod_expires.

Code for mod_expires in an <ifmodule>

Using mod_expires in an <ifmodule> allows image types and other file types to be set in an array. This matches the file types to the specific expire time. This streamlines the htaccess code. In the code below, the file types are listed in a row like jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3. Below is an example of the code to use.

<ifmodule mod_expires.c>
<Filesmatch "\.(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$">
    ExpiresActive on
    ExpiresDefault "access plus 2 days"
</Filesmatch>
</ifmodule>

This sets the cache to expire to the same duration for each of the file types. To specify the specific times for each individual file type, you will want to use the previous code example. The following will explain the Directive types that are used for Cache-Control.

Directive types

There are three directive types; ExpiresActive, ExpiresByType, and ExpiresDefault. The following table explains the difference between them.

Caching directives
ExpiresActive Directive Enables the Expires headers for the website.
ExpiresByType Directive This defines the age of the cache header and the type of file to cache.
ExpiresDefault Directive This sets the age of the cache for all documents other than those specified in the ExpiresByType for the site.

Caching Directive bases

There are 3 base types the access, now, and modification type.

  • access
  • now (same as “access“)
  • modification

Duration of cache time

The duration of cache time can be set to one of the following units of time.

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

Basic syntax for ExpiresByType intervals

The following shows the basic syntax for the ExpiresByType. Each file type can be set to specific times to expire the cache.

ExpiresByType text/html "access plus 2 days 12 hours"
ExpiresByType image/png "access plus 6 months 3 days"

Recommended Expire date ranges

You do not want to set your expire times for your cache to unrealistic settings. If you keep the cache to a maximum of a year and a minimum of a month, you should have your browser caching working optimized for your site.

  • Set your images to a long expire time like “access plus 1 year”. Images take more time to load and are updated less frequently than other files.
  • Make your CSS, HTML, and Javascript expire at a minimum of a month like “access plus 1 month”. CSS, HTML and JavaScript’s typically are updated more when developing a site than the sites images.
  • Keep your cache expire date at most a year.
Nitesh Shah

Share
Published by
Nitesh Shah
Tags: apache

Recent Posts

How to setup first Azure Virtual Machine?

Setting up your first Azure Virtual Machine can be done by following these steps: Create…

1 year ago

How to setup Amazon Cloudfront and S3 to serve static resources

Amazon CloudFront is a content delivery network (CDN) that helps you serve static content such…

1 year ago

Step-By-Step Guide To Setting Up An AWS Application Load Balancer

Step-By-Step Guide To Setting Up An AWS Application Load Balancer Are you looking for a…

1 year ago

How to restore MySQL database from .frm and .ibd files?

MySQL databases often get corrupted due to issues like hardware failure, file system failure etc.…

4 years ago

SQL Server Replication

SQL Server Replication is the process of copying databases from one node to another to…

5 years ago

101 System Admin Tools to make life easy

Here are 101 System Admin tools which make System Admins' life easy.

7 years ago

This website uses cookies.