How To Enable mod_expires To Cache Your Website Files For Speed?

Mod_Expires is a built-in module for the Apache web server, available on all Sangkrit.net hosting plans. This module leverages the functionality of mod_expires to manage how long a client’s web browser caches website resources.

Here’s how it works: Mod_Expires manipulates the “Expires” headers sent by the hosting server. This gives you control over how long a browser stores cached copies of website resources. By adjusting these cache durations, you can optimize your site’s performance while considering your website’s update frequency.

Important Note: If the server response already includes an “Expires” header (e.g., generated by a CGI script or a proxied origin server), Mod_Expires won’t modify or add an “Expires” or “Cache-Control” header in those cases.

How To Enable mod_expires Via .htaccess?

If Mod_Expires is not enabled you will need to enable it in your .htaccess file which is present in your website’s root directory, sometimes hidden and can be viewed by changing the view option from the top right corner of cPanel’s file manager. You can enable it on a per-directory basis and it applies recursively.

Sangkrit.net uses the vendor-provided code from Apache mod_expires. Apache HTTPD documentation is accurate for using .htaccess files on your hosting account.

Locate your .htaccess file: This file is usually located in your website’s root directory (e.g., /var/www/html/). The following given example enables mod_expires for a set of common image types and CSS files:

# Activate mod_expires for this directory
ExpiresActive on
# locally cache common image types for 7 days
ExpiresByType image/jpg "access plus 7 days"
ExpiresByType image/jpeg "access plus 7 days"
ExpiresByType image/gif "access plus 7 days"
ExpiresByType image/png "access plus 7 days"
# cache CSS files for 24 hours
ExpiresByType text/css "access plus 24 hours"

Similarly, you can configure mod_expires in your .htaccess file with more options by adding extra caching rules.

Open the .htaccess file and add the following code to specify the caching duration for different file types:

<IfModule mod_expires.c>
    ExpiresActive On
    # Set expiration for images
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    # Set expiration for CSS and JavaScript
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType text/javascript "access plus 1 week"
    # Set expiration for other file types
    ExpiresByType text/html "access plus 1 day"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresDefault "access plus 1 week"
</IfModule>

How To Enable mod_expires Via Apache Server?

Alternatively, if you have root or administrative access to your server you can enable mod_expires directly in Apache by using the command line. Simply, access your server and do as directed.

Step-1 Enable the module by running the following command:

bash
sudo a2enmod expires

Step-2 After enabling the module, restart Apache to apply the changes. Simply use the following command for restarting apache :

bash
sudo service apache2 restart

Now clear your browser cache to ensure you see the latest changes.

You may also check HTTP headers by using browser developer tools (Network tab) to confirm that the “Expires” or “Cache-Control” headers are set correctly for the specified file types.

By enabling mod_expires, you can significantly reduce page load times, as browsers will cache static files and serve them locally on subsequent visits.

How To Enable Mod_Expires Via WordPress Plugins?

WordPress users may also use some cache plugins such as WP Super Cache or W3 Total Cache to get this thing done from their admin area dashboard. Read this lesson to know the complete process.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.