Enable browser caching in WordPress using htaccess

Browser caching for all types of files can be easily done using Apache's .htaccess file. This simple configuration results in a faster website, less consumption of bandwidth and better user experience.

Browser caching is very important for any website as it helps in speeding up the website as well as reduces bandwidth costs.

To enable browser caching on Apache, you must have the required modules enabled.

If you are on a shared host like GoDaddy or HostGator, these modules are already enabled in most cases. So, you can skip the following steps and directly go to the htaccess code given at the end of this post.

In case, you are on a cloud server like DigitalOcean, you may need to enable these modules which can be done in following easy steps.

Open your terminal and run following two commands.

sudo a2enmod headers
sudo a2enmod expires

Now restart the Apache server by running following command.

systemctl restart apache2

It will ask for authentication. Enter your password and Apache will restart.

You have successfully enabled Headers and Expires modules on your Apache server.

Now, open the root folder of your website and create a .htaccess file with the following content.

# BEGIN cache-control
<IfModule mod_expires.c>;
    ExpiresActive on
    ExpiresDefault "access plus 2 days"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/x-ico "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"
    ExpiresByType text/html "access plus 1 seconds"
    ExpiresByType text/xml "access plus 1 seconds"
    ExpiresByType text/plain "access plus 1 seconds"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/xml "access plus 1 seconds"
    ExpiresByType application/rss+xml "access plus 1 seconds"
    ExpiresByType application/json "access plus 1 seconds"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
  <IfModule mod_headers.c>;
       Header unset ETag
       Header unset Pragma
       Header unset Last-Modified
       Header append Cache-Control "public, no-transform, must-revalidate"
       Header set Last-modified "Tue, 1 Oct 2014 10:10:10 GMT"
# END cache-control

And that’s it. You do not have to restart your server now as .htaccess file works in real-time. All the files served from your server will now be cached in the user’s browser as per the expires directive mentioned in the .htaccess file. You may also change these to any value of your liking.

By Abhineet Mittal

Abhineet is IIM Alumnus and Founder of popular Digital Publishing Platform, Sahityapedia. He is a technology enthusiast by hobby & an avid reader.

Leave a Reply

Your email address will not be published.