+2 votes
in Web & Google by (71.8k points)
When I test my website on google pagespeed, it flags "Leverage browser caching" warning for image and javascript files. How can I fix it so that the score goes up?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

If you have access to the configuration file, you can put the following codes in httpd.conf file and restart apache. If you do not have access, you have to put the codes in .htaccess file. If you are modifying .htaccess, you do not have to restart apache.

Also, you can create a new file empty.conf in folder /usr/local/apache/conf.d and put these codes in that file. You need to restart apache if you create this new file.

LoadModule expires_module modules/mod_expires.so

# BEGIN Expire headers

#The below codes will instruct the browsers to cache the contents mentioned below. 

<IfModule mod_expires.c>

    ExpiresActive on

    ExpiresDefault A0

    ExpiresByType image/webp "access plus 60 days"

ExpiresByType image/jpg "access plus 60 days"

ExpiresByType image/jpeg "access plus 60 days"

ExpiresByType image/gif "access plus 60 days"

ExpiresByType image/png "access plus 60 days"

ExpiresByType image/x-icon "access plus 60 days"

ExpiresByType image/icon "access plus 60 days"

ExpiresByType application/x-shockwave-flash "access plus 60 days"

ExpiresByType application/pdf "access plus 60 days"

ExpiresByType image/svg+xml "access plus 60 days"

ExpiresByType text/css "access plus 30 days"

ExpiresByType application/x-javascript "access plus 30 days"

ExpiresByType text/javascript "access plus 30 days"

ExpiresByType application/javascript "access plus 30 days"

ExpiresByType application/vnd.ms-fontobject "access plus 30 days"

ExpiresByType application/x-font-ttf "access plus 30 days"

ExpiresByType application/x-font-opentype "access plus 30 days"

ExpiresByType application/x-font-woff "access plus 30 days"

ExpiresByType application/font-woff2 "access plus 30 days"

</IfModule>

# END Expire headers

You can change the number of days depending on how many days you want. This is my configuration.


...