Using the mod_expires module
Apache’s mod_expires module enables you to define expiration intervals for different types of content on your web site. For example, you could use mod_expires directives to instruct browsers to cache image files for one hour, JavaScript files for two weeks, and CSS files for two months. The following sample .htaccess configuration demonstrates how to do this:- The ExpiresActive directive is set to On, which instructs Apache to generate Expires and Cache-Control HTTP response headers for the specified content types. Web browsers parse these HTTP response headers to determine how long to cache content on the client.
-
The ExpiresByType directives define the expiration periods for specific types of content. You can specify expiration periods in seconds, minutes, hours, days, weeks, months, and years. To define a specific type of content, use the MIME type, such as text/html or image/png. For a list of MIME types, please visit https://en.wikipedia.org/wiki/Internet_media_type.
📘 Note There are actually two ways to define expiration periods. You can specify an expiration period from the time the browser accesses the file by using the access keyword. Alternatively, you can specify an expiration period from the time the file was last modified on the server by using the modification keyword.
- The ExpiresDefault directive is an optional directive that specifies the expiration period for all other types of files not explicitly set in an ExpiresByType directive. In this example, any file that is not an image, JavaScript, or CSS file expires in two days.