Speed up website -Compress CSS/JavaScript using GZIP/DEFLATE compression – Optimization Tips -Part 2

In our previous discussion we checked the factors which influence the website load speed.(Read : 7 steps to Speed up website loading – Website Optimization Tips -Part 1).

Beginning with HTTP/1.1, web clients indicate support for compression with the accept-encoding header in the HTTP request.The web client can sent the following values as the accept-encoding header.

Accept-Encoding: gzip, deflate

Gzipping generally reduces the response size by about 70% and the result will be a faster and quicker web site.
Read: 7 steps to Speed up website loading – Website Optimization Tips -Part 1
Read: Check website/ web page for css/script Gzip/ Deflate compression

How do we achieve CSS or JavaScript compression?
The HTTP request header which indicates the encoding types is “Accept-Encoding: gzip, deflate”.
If the web server sees this header in the request, it may compress the response using one of the methods listed by the client.How do we achieve this?


Majority of the websites are hosted on Apache Web Server.The Apache server by default will not enable GZip compression. We have to manually achieve this by editing the .htaccess file.
First thing to check here is to find out whether your server supports mod_gzip or mod_deflate.if your server support this then you can add few lines of code to your .htaccess file which will enable the compression for you.

Usually you can find the .htaccess file in the root location of your web context. It is the httpdocs (/htdocs or /public_html) directory.If it does not exist create an .htaccess file and upload to that directory using an FTP tool like FileZilla.
As we discussed in our previous post the important thing to note here is that ,if you use Apache web server, the module configuring gzip depends on your version.
Apache version 1.3 uses mod_gzip but Apache 2.x uses mod_deflate.

How to use Mod_Deflate or DEFLATE method to support the GZip compression.
This is simple and straight forward.Just add the following line to the .htaccess file.

# compress the files:
AddOutputFilterByType DEFLATE text/html text/plain text/xml

If you want to have additional types of files you add those to the end of the line as shown below.

# compress the files:
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/x-javascript

Using mod_gzip or GZIP method to support the GZip compression.
Apache version 1.3 uses mod_gzip and you should add a slightly different scripts to .htaccess file to achieve GZIP compression.
Copy and paste the following code to the .htaccess file.

# gzip compression

mod_gzip_on       Yes
mod_gzip_dechunk  Yes
mod_gzip_item_include file      .(html?|txt|css|js|php|pl|jpg|png|gif)$
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.*

Check the code once ,here you can include or exclude certain file types compression.
Now you can check the speed of your website and verify whether the CSS and script files are served using GZIP encoding or not.
Is there any utility available to check whether your website is optimized for Data compression using GZIP or INFLATE?

Read that Here:Check website/ web page for css/script Gzip/ Deflate compression

But what if your webserver has ZLIB enabled?
Some web servers supports ZLIB and it is enables for them.Here is the tip for those types of web servers.This is also an easy one, simply add the following to your .htaccess file


# compress the files:
php_flag zlib.output_compression On

Static compression methods.
You can instruct the server to serve static zipped version of the script or css file, if it finds one, instead of the normal file.If you add the following line to .htaccess file the server will do just that.

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+) $1.gz [L]

4 thoughts on “Speed up website -Compress CSS/JavaScript using GZIP/DEFLATE compression – Optimization Tips -Part 2

  1. do you need to put the compression code on the index pages themselves that you want compressed?
    how does putting them in the htaccess file know what pages to compress?

  2. Thanks for this article…
    it will be useful for me

    let me save this page…
    just for offline
    so i can read this at my home

    So by this comment, i say thankss for your great article

  3. Thanks for this article…
    it will be useful for me

    let me save these pages…
    just for offline
    so i can read this at my home

    So by this comment, i say thankss for your great article

Leave a Reply

Your email address will not be published. Required fields are marked *

Shares