What is GZIP Compression

Compression is an act of reducing file size sent from your hosting server. Compression allows your web server to provide smaller file size that can speed up your website loading time upto 70% and it saves at least 50% of your bandwidth usage.

GZIP Compression is recommended for following type of files

  1. Static HTML (such .html, .htm)
  2. Dynamic HTML (such as extension .php, .aspx) 
  3. Textfiles (extension .txt) 
  4. Layout & Script Files (such as extension .css and .js) 
  5. Webservices, Files (such as WSDL, REST and JSON)

GZIP compression is not recommended for non-text files, such as media or graphical files because it is not save much space when compressing.

Importance of GZIP Compression

Enabling compression is very much important in today’s web world. If you are not using compression means you are losing your visitors because due to slower page speed your competitors get more visitors if they have enabled compression. All large websites worldwide use GZIP Compression.

How Compression Works on Web

When a user (client browser) requests a page on a webserver, it compresses (zip) textfiles and sends the files to client browser. Client browser automatically unzips files and serves the user much faster. All modern browsers understand and accept compressed files.
When a browser visits a webserver it checks to see if it's enabled it receives the gzip file which is significantly smaller and if it is not, then it still receives the normal page.

How to Enable GZIP Compression

First you check either compression is installed on your hosting server or not (assuming your host server using apache you'll need the mod_gzip.c OR mod_deflate.c modules installed, or you can check with phpinfo(); function)

Compression can be enabled by configuring your webserver.

  1. Each web servers has its own setting for enabling configuration
  2. Here are the most common ways to enable compression

For Apache Web Server

In apache web server, there are two modules (mod_gzip.c or mod_deflate.c) to enable GZIP compression. Below mention code give you an example of mod_deflate. You will need to add the following lines of code to your .htaccess file

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  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/svg+xml
  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
  Header append Vary User-Agent
</IfModule>

or

#compress all text & html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
# Or, compress certain file types by extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>

Save the .htaccess file and then refresh your webpage. Check either compression is working properly visit www.checkgzipcompression.com Enter your website address and test if your web server is GZIP enabled.

For IIS

In IIS, you can set up compression by setting server properties.

Go to "website properties" (Tab Services), and then allow "compress application files".