Leverage Browser Caching for Images, CSS and JS

To leverage your browser's caching generally means that you can specify how long browsers should keep images, CSS and JS stored locally. Of course, if you change any of those, the server will tell the visitor's browser to clear that cache. This piece of code can make a huge difference and will reduce the number of HTTP request, which is a huge benefit for any returning visitors to your website. To enable it, add those lines to 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 application/pdf"access 1 month"
ExpiresByType application/x-javascript"access 1 month"
ExpiresByType application/x-shockwave-flash"access 1 month"
ExpiresByType image/x-icon"access 1 year"
ExpiresDefault"access 2 days"
</IfModule>
## EXPIRES CACHING ##


In simpler terms when a customer visits your Magento website for the first time, their browser will then fetch all of your images, css files, javascript files, etc  found on your website.  In a normal situation that happens every time that visitor visits your Magento website.

With Expires headers added to you htaccess file you will be informing their browser that the files you specify are not changing until after a set time. This means that your customer’s browser won’t have to fetch everything, every time they visit your Magento website.

We have set them to expire in a month, however you can choose a longer or shorter time scale if you wish.

It might be worth looking at each individual file individually and determine how often these files could be changed, from there make a decision how long they can be cached to your customers web browser. You have 7 options:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Block a country using mod_geoip (.htaccess)

Do you have a list of countries that you want to block access to your site?  Have you tried...