PageSpeed : Enable compression

Published

Compressing resources with gzip or deflate can reduce the number of bytes sent over the network.


What is Compression?


Compression is the first step to speed up your website. most web browsers supports gzip compressed contents to be served and decompressed in browser itself.

How to enable compression ?

Below , I have listed  solutions for two most common servers. Apache server and IIS Server.  Make sure you make backup for configuration files before editing it.

Using .htaccess in Apache Server


mod_gzip configuration for Apache 1.3.x


<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php)$
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>


mod_deflate configuration for Apache 2.x


<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
# Don’t compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>
</IfModule>



Enable Compression in IIS7


You can do this in you Web.config file

<system.webServer>
  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
      <add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="*/*" enabled="false"/>
    </dynamicTypes>
    <staticTypes>
      <add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="*/*" enabled="false"/>
    </staticTypes>
  </httpCompression>
  <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>


Testing whether gzip compression works or not.

You can use developer tools in Google chrome or FireBug in Firefox to see whether gzip compression works or not.
If you see Content-Encoding : gzip in response headers, it works fine.

Or you can use Google's pagespeed analysis to check whether gzip compression works or not.


Comments

Post a Comment