What is mod_expires?
Mod_expires is an Apache module that instructs web browsers on how to manage the cache. The mod_expires values inform web browsers about how long to store images, HTML files, and so on. Such configurations will result in quicker page load times for subsequent requests from the same user.
The mod_expires module manages the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can be specified to be relative to either the last time the source file was last modified or the time the client accessed the file.
Apache mod_expires page
Use the following code to verify if the expires module is loaded:
httpd -M | grep expires
#[root@web /]# httpd -M | grep expires expires_module (static) [root@web /]#
Configuring mod_expires rules
You can configure mod_expires rules in the .htaccess file from the root directory of your site.
Example:
<IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType text/css "access plus 1 month" ExpiresByType application/pdf "access plus 1 month" ExpiresByType text/x-javascript "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" ExpiresByType image/x-icon "access plus 1 year" #default expiry time ExpiresDefault "access plus 2 days" </IfModule>
You can add or remove MIME types as per your needs. You can use the above as a template.
The objective is to specify which MIME types should be cached. Begin each line with ExpiresByType, then MIME type followed by time period.
Testing if mod_expires works
Use the curl command to access a file from the site if you have SSH access.
curl -is https://example.in/file
For accessing a png file, here is an example:
[root@web /]# curl -is https://demo.milesweb.in/logo.png HTTP/2 200 date: Tue 9 November 2021 14:40:22 GMT server: Apache/2 last-modified: Fri, 10 February 2018 23:58:37 GMT etag: "244c-4d58fcdd26540" accept-ranges: bytes content-length: 9292 cache-control: max-age=31536000 expires: Wed, 9 November 2022 14:40:22 GMT content-type: image/png [root@web /]#
The above example explains that the server is instructing the client to cache the file for one year i.e. from the present date till 9 November 2022.
Related Article: Learn to configure caching with the mod_expires module
That is how you can manage the cache on your web browser using Apache module mod_expires.