Knowledge Base
MilesWeb / How-Tos

Learn to configure caching with the mod_expires module

Approx. read time : 3 min

This article covers the introduction to Apache’s document expiration functionality provided by the mod_expires module.

By setting up the expiration periods for web content allows your web browser to cache the content for a specific time period. Therefore, it allows you to reduce the number of HTTP requests that the web server must process, which helps in improving web site speed and performance.

# How to use the mod_expires module?

Using Apache’s mod_expires module you can define expiration intervals for different types of content on your website. For example, it allows you to use mod_expires directives to instruct browsers to cache image files for one hour, or JavaScript files for two weeks and also CSS files for two months.

Following is the sample .htaccess configuration that shows how to do it :

In the above example :

• You can see that the ExpiresActive directive is set to On. This instructs Apache to generate Expires and Cache-Control HTTP response headers for the specific content types. Web browsers than resolve these HTTP response headers to determine how long to cache content on the client.

• The ExpiresByType directive denotes the expiration periods for the specific types of content. You are allowed to specify the expiration time in seconds, minutes, hours, days, weeks, months and also in years. In order to define a specific type of content, use the MIME type, like text/html or image/png. You can find more MIME types here https://en.wikipedia.org/wiki/Media_type.

• The ExpiresDefault directive is an optional directive that defines the expiration period of all other types of files and not explicitly set in an ExpiresByType directive. In this example, any file which is not an image, JavaScript or CSS file gets expire in two days.

Note: You can if verify your expiration settings in the .htaccess file are working correctly or not, by examining the raw HTTP headers that are sent between the browser and web server.
(To perform this, use a browser plugin that is able to display the raw headers like Live HTTP headers for Mozilla Firefox, or the Developer Tools feature in Google Chrome). When the content gets marked for expiration, Apache itself adds the following lines to the HTTP response header (the exact values will vary based on your own .htaccess settings):

Cache-Control: max-age=86400
Expires: Thu, 02 Oct 2014 19:02:30 GMT

Also Read :

1) How To Enable Caching In Drupal?

2) How To Fix Leverage Browser Caching In WordPress Website

Sonam Wagh
With an interest in doing something creative daily, Sonam works as a Digital Marketing Executive. She likes to write technical blogs related to web hosting, digital marketing, and other IT topics. She also likes to spend her leisure time on social media to find different strategies for client engagement.
Need help? We’re always here for you.
DON'T LOSE YOUR FREE TRIAL
OFFER EXPIRES IN
04
min
59
sec
Who is your preferred cloud partner?
Let us know and we’ll manage it for you
Amazon Cloud
Digital Ocean Cloud
No, thank you