Gzip compression – to make website load faster

Now a days Gzip compression become popular for speed up website. It compresses webpage upto 60% to 80%. We know about zipping a file to make them use less space, today web servers can also compress the content before sending it to your browsers. It lower the bandwidth requirements of your server and speed up your site.


How Gzip works? Click Here to watch video


If mod_deflate and mod_gzip modules is enable on server, Here is the code you should added to your .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 >


If the above code did not seem to work, do not afraid there is other way to do it, remove above code from your .htaccess file and add this code in .htaccess file.

< IfModule mod_deflate.c >

# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css

# Level of compression (Highest 9 – Lowest 1)
DeflateCompressionLevel 9

# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html

< IfModule mod_headers.c >
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

< /IfModule >
< /IfModule >


Some hosting provider disable mod_deflate and mod_gzip modules, so above code will not work for you. Here is the solution.


1) Create file called headers.php and add the following code, upload to public_html or htdocs folder.

< ? php
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$timestamp = filemtime(__FILE__);
header('Last-Modified: ' . $timestamp);
$expirestime = 60*60*24*30;
header("Pragma: public");
header("Cache-Control: maxage=".$expirestime);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expirestime) . ' GMT');
header('Vary: Accept-Encoding');
$pathinfo = pathinfo($_SERVER['SCRIPT_FILENAME']);
$fileextension = $pathinfo['extension'];
if ($fileextension == 'css') {
header('Content-type: text/css;
if ($fileextension == 'js') {
header('Content-type: text/javascript;
} ? >


2) Create another file and give the name php.ini and add following code in it.
auto_prepend_file=[PATH OF headers.php FILE]

if zlib.output_compression=1 is not work try utput_handler=ob_gzhandler instead.
Now you need to upload copy of php.ini file in every folder containing files you want to compress.


3) Now add below code in your .htaccess file
AddType x-mapp-php5 .php .shtml .html .htm .txt .js .css
if you have php4 then add x-mapp-php4 instead x-mapp-php5
if x-mapp-ph

p5 not working use php5-script instead of x-mapp-php5.




Now test your web page whether it compressed or not. Here are some online tools for compression test.