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?
Following is the sample .htaccess configuration that shows how to do it :
ExpiresByType image/png "access 2 hour"
ExpiresByType image/gif "access 2 hour"
ExpiresByType image/jpeg "access 2 hour"
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.
(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):
Also Read :