How to enable browser caching & gzip compression on Apache webserver

How to enable browser caching & gzip compression on Apache webserver

Page speed is vital for SEO. The faster your website or web page loads, the better chance of it having better position in the search results. To measure and get guideline on what to be done to get faster website, you can use online tools like Google PageSpeed Insights, GTMetrix or WebPagetest.

Each tool will probably give you a handful of suggestions in how to improve your site’s speed. However there are 2 most essential components that you need to be sure have been implemented on your website or server. They take only a few minutes to set up but give the most noticeable impact to the site’s performance.

If you are using Apache webserver, below are the instructions to add them.

Enable browser caching

Add this into your .htaccess file:

## ADD BROWSER 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>

Enable gzip compression

Add this into your .htaccess file:

## ADD GZIP COMPRESSION ##
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
Header append Vary User-Agent env=!dont-vary

Sometimes due to the server’s configuration, the gzip compression works on every other file except CSS and JS files. If this happens, you could try adding this before the ## ADD GZIP COMPRESSION ##:

## TYPES FIX ##
AddType text/css .css
AddType text/javascript .js

 

That’s it. Give the site another test on page speed tools and see how much the score has improved.

Leave a Reply