As in Magento 1 to increase the page speed score and Y-slow score we have used Leverage Browser Caching for Images, CSS and JS code in .htaccess file but same script in Magento2 is not working.
below is the code of browser cache:
# Leverage browser caching using mod_expires #
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access plus 1 year"
 ExpiresByType image/jpeg "access plus 1 year"
 ExpiresByType image/gif "access plus 1 year"
 ExpiresByType image/png "access plus 1 year"
 ExpiresByType text/css "access plus 1 month"
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType text/x-javascript "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access plus 1 month"
 ExpiresByType image/x-icon "access plus 1 year"
 ExpiresDefault "access plus 2 days"
</IfModule>
# End of Leverage browser caching using mod_expires #
Why Magento 2 is not considering this code.?
If anything is wrong in code please share the code so we can optimize the code.
- 
 are you using shared hosting?Amit Naraniwal– Amit Naraniwal2019年02月08日 10:12:12 +00:00Commented Feb 8, 2019 at 10:12
- 
 No,we are using AWS dedicated EC2.akgola– akgola2019年02月08日 10:13:44 +00:00Commented Feb 8, 2019 at 10:13
- 
 you can try this one fishpig.co.uk/magento/optimisation/leverage-browser-cachingAmit Naraniwal– Amit Naraniwal2019年02月08日 10:15:31 +00:00Commented Feb 8, 2019 at 10:15
- 
 This is also not working.akgola– akgola2019年02月08日 10:23:46 +00:00Commented Feb 8, 2019 at 10:23
- 
 do you have apache mod_expires?MagenX– MagenX2020年03月09日 08:41:39 +00:00Commented Mar 9, 2020 at 8:41
2 Answers 2
I had also faced the similar issue, even after updating .htaccess caching was not applied to files. Enable mode expires module and restart apache2, fixed issue.
sudo a2enmod expires
sudo service apache2 restart
Add the following at the following:
root> .htaccess file
# Leverage browser caching using mod_expires #
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access plus 1 year"
 ExpiresByType image/jpeg "access plus 1 year"
 ExpiresByType image/gif "access plus 1 year"
 ExpiresByType image/png "access plus 1 year"
 ExpiresByType text/css "access plus 1 month"
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType text/x-javascript "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access plus 1 month"
 ExpiresByType image/x-icon "access plus 1 year"
 ExpiresDefault "access plus 2 days"
</IfModule>
# End of Leverage browser caching using mod_expires #
It will be clear the Leverage browser aching issue.
- 
 The steps you suggest for increasing the Magento 2 speed right?zus– zus2020年10月03日 06:47:35 +00:00Commented Oct 3, 2020 at 6:47