Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  How-Tos  >  Learn to configure caching with the mod_expires module
Top Scroll

Learn to configure caching with the mod_expires module

 2 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 :

ExpiresActive On
ExpiresByType image/png "access 2 hour"
ExpiresByType image/gif "access 2 hour"
ExpiresByType image/jpeg "access 2 hour"
ExpiresByType text/javascript "access 2 weeks"
ExpiresByType text/css "access 2 months"
ExpiresByType text/html "modification 4 hours"
ExpiresDefault "access 2 days"

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

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.