Leverage browser caching for .htaccess

59 / 100

How to do Leverage browser caching for .htaccess. Below code will tells browsers what to cache and how long to “remember” it a browser.  Top of your .htaccess file place below code.

Leveraging browser caching can significantly improve the speed and performance of your website. Here’s how you can set up browser caching for your website’s .htaccess file:

  1. Access your website’s .htaccess file: Log in to your website’s server using an FTP client and locate the .htaccess file in your website’s root directory.
  2. Add the code to enable caching: Copy and paste the following code into your .htaccess file:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

How to Control your Cache?

Use below code in your .htaccess file.

# 1 Month for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

This code specifies how long specific types of files should be cached by the browser.

  1. Save the changes: Save the .htaccess file and upload it to your website’s server.

Once you have completed these steps, your website’s visitors will be able to load your website’s pages faster as their browsers will have stored certain files locally on their machines.

Comments

comments