One of the things that Google PageSpeed uses to rate your site, and your position in the Google results, is how effectively it caches static assets. That’s thing like images, JavaScript files, etc. that don’t really change once your site’s gone live.

Google PageSpeed cache results

Look familiar?

Setting an efficient cache policy not only improves your PageSpeed score but also improves browsing for you users, which is the main goal. It tells their browser to keep a local copy of static assets for a certain amount of time, which means they’re not having to download images and stylesheets every time they visit your site – they can just use the copy they downloaded last time that’s still on their hard drive.

To set up a Google-friendly caching policy all you need to do is paste the following into your .htacess file in your website root (e.g. in the public_html folder).

You’ll need mod_deflateĀ and mod_expires enabled in your Apache config to set this up.

On Ubuntu/Debian servers you can run

apachectl -M

to get a list of the enabled modules. If either of those two are missing, enable them using

a2enmod deflates expires

and then restart Apache

service apache2 restart

Here’s what needs to go into the .htaccess file

<ifmodule mod_deflate.c> 
# Compress HTML, CSS, JavaScript, Text, XML and fonts 
AddOutputFilterByType DEFLATE image/svg+xml 
AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
AddOutputFilterByType DEFLATE application/x-font 
AddOutputFilterByType DEFLATE application/x-font-opentype 
AddOutputFilterByType DEFLATE application/x-font-otf 
AddOutputFilterByType DEFLATE application/x-font-truetype 
AddOutputFilterByType DEFLATE application/x-font-ttf 
AddOutputFilterByType DEFLATE application/x-javascript 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE font/opentype 
AddOutputFilterByType DEFLATE font/otf 
AddOutputFilterByType DEFLATE font/ttf 
AddOutputFilterByType DEFLATE image/x-icon 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/xml 
# Remove browser bugs (only needed for really old browsers) 
BrowserMatch ^Mozilla/4 gzip-only-text/html 
BrowserMatch ^Mozilla/4\.0[678] no-gzip 
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</ifmodule>

<IfModule mod_expires.c> ExpiresActive on
# Set the default expiry times.
#ExpiresDefault "access plus 2 days"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 6 month"
ExpiresByType text/javascript "access plus 6 month"
ExpiresByType application/javascript "access plus 6 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year" 
</IfModule>



A full list of MIME types that you can use with ExpiresByType headers is available here https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types

Let's Get In Touch!


Ready to start your project? Get in touch to have a chat!