Enabling GZIP compression using htaccess

Enabling GZIP compression can help you reduce the size of your webpage, which can reduce the amount of time to download the resource of webpage, reduce data usage for the client and improve webpage speed.

What is GZIP Compression?

GZIP simply is a file format and used for file compression and decompression. GZIP compression is enabled server-side, and allows user to reduce the file size of your HTML, stylesheets, and JavaScript files. It will not work on images as these are already compressed in a different way but we can enable cache to reduce webpage size for return users . Using enabling GZIP file compression we can reduce up to 70% file size. It is easiest way to optimizations of webpage.

How to Enable GZIP Compression

Here i am enabling GZIP Compression using htaccess file, this code will be enable GZIP and cache of webpage but here i am not setting expiry date of cache file for use it copy and past into htaccess file.

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

Enable GZIP Compression With Expiry Date

Here i am enabling GZIP Compression using htaccess file, this code will be enable GZIP and cache of webpage with expiry date of cache file for use it copy and past into htaccess file.

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 11 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
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 month"
ExpiresDefault "access plus 2 days"
</IfModule>

Note : Don’t set expiry date for long term because of if you set cache expiry date 1 year then you made changes in css file or website design then for return user not visible your latest version or design of website.

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.