How to enable GZIP Compression for web page speed

Abhigyan Singh 12th Jul 2020

This tutorial will explain about GZIP Compression for our website. As we know that compression means to compress the file size. So the loading time of web page is fast. Compression permits our web server to supply smaller file sizes that load quicker for your web site users. We can compress our static files like HTML, CSS and javascript with gzip that can save our bandwidth upto sixty to seventy percent of the actual file size. This means that it takes less time to load our web pages. Nowadays every website uses GZIP Compression technique to reduce the file size. Now most of the search engine suggests enabling GZIP compression technique to our website.
You can check your website GZIP compression using GOOGLE Page Speed.

Widely used search engine like GOOGLE, YAHOO accept this compression technique. Every modern browsers except older version of IE accepting this compression because they want to enjoy their user with latest CSS style and javascript. In present time most internet users are technical and they don’t want to see the website is just loading and it takes more time. So this gzip compression technique helps us to improve our web page speed.

How does it work?

Suppose we want to https://discussdesk.com//. So when our client(Browser ) gives the request for this page to our webserver. Then web server respond with response code 200 OK and send the file with all supported file in actual file size to the client.


After using compression technique, it will reduce our website file size and when any client request for that page, then our web server serves the compress file that will get load fatser.

How to enable GIZP Compression

GIZP Compression can easily be enabling by just adding some code to our .htaccess file. Just add the below code to your .htacces file and save it.

<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>

<FilesMatch "\\.(js|css|html|htm|php|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=31536050"
</FilesMatch>

Now just refresh your web server and check GZIP Compression from compression testing tool.

In Apache, there are two compression options are available

  • mod_deflate is standard technique and easier to set up.
  • mod_gzip is more powerful and can compress more. This is the recommended compression technique.

If you are unable to access the .htaccess file or you cannot change or rewrite the .htaccess file then you can directly write a simple PHP code to common file on the top of your web page.

<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler");
else
ob_start(); ?>

This also send Accept-encoding with the header and compress the file but better to use Apache GZIP compression.

Now you can check for GZIP compression online.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk