This article provides an overview of Apache's document expiration functionality, as supplied by the mod_expires module.

Setting expiration dates for web content allows web browsers to cache content for certain time periods. As a result, the amount of HTTP requests that the web server must process is reduced, which enhances web site speed and performance.

Using the mod_expires module

You can establish expiration intervals for various sorts of content on your website using Apache's mod_expires module. For example, you could instruct browsers to cache image files for one hour, JavaScript files for two weeks, and CSS files for two months using mod_expires directives.

The following sample .htaccess configuration demonstrates how to do this:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/png "access 1 hour"
    ExpiresByType image/gif "access 1 hour"
    ExpiresByType image/jpeg "access 1 hour"
    ExpiresByType text/javascript "access 2 weeks"
    ExpiresByType text/css "access 2 months"
    ExpiresByType text/html "modification 4 hours"
    ExpiresDefault "access 2 days"
</IfModule>

In this example:

  • The ExpiresActive directive is set to On, instructing Apache to emit Expires and Cache-Control HTTP response headers for the content types provided. These HTTP response headers are parsed by web browsers to determine how long to cache content on the client.
  • The ExpiresByType directives specify the expiration period for various categories of information. Expiration times can be specified in seconds, minutes, hours, days, weeks, months, and years. Use the MIME type to specify a certain type of content, such as text/html or image/png.
  • The ExpiresDefault directive is optional and sets the expiration period for any other types of files that are not expressly provided in an ExpiresByType directive. Any file that is not an image, JavaScript, or CSS file expires in two days in this case.
Was this answer helpful? 0 Users Found This Useful (0 Votes) apache web server, mod_expires module