Abhineet.in

unlearning the learnt

Menu
  • Home
  • About
  • CAT Preparation
Menu

Enable browser caching in WordPress using htaccess

Posted on August 6, 2019April 5, 2023 by Abhineet Mittal

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 Webroris.com 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"
  </IfModule>;
</IfModule>;
# END Webroris.com 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.

Category: WordPress

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Business
  • CAT Preparation
  • Coding
  • Fiction
  • Finance
  • How-To-Life
  • Javascript
  • Life
  • Life at Infosys
  • Photoshop
  • Productivity
  • Publishing
  • Reading
  • Social Awareness
  • Startup
  • Technology
  • Uncategorized
  • Utilities
  • Visual Studio Code
  • Web Development
  • WordPress
  • Work Culture

Recent Posts

  • Book Review: The Great Gatsby
  • The Ultimate Guide to CAT Preparation: Tips and Tricks
  • Self-Publishing vs. Traditional Publishing: Which Is Right for You?
  • 7 Effective Strategies to Overcome Procrastination and Achieve Your Goals
  • How to detect English/ASCII string in PHP

Recent Comments

  • Rohan George on Intermediate, Stream & Location: Luck by Chance
  • Ashika on Intermediate, Stream & Location: Luck by Chance
  • Steve on My first job, Infosys
  • SAKTHI on Finally, its Mysore…
  • Akash dhyani Akash on Training Begins…
© 2023 Abhineet.in | Powered by Minimalist Blog WordPress Theme