Saturday, 19 October 2013

How To Enable PHP Gzip Compression On Your Website?

PHP Gzip Compression
I was trying the Gzip compression level on this website from the morning. But the website was not getting gzip compressed. I do not know the reason but the gzip testing tools on the internet were saying the website is not gzipped.



Enable Gzip Compression by .HTACCESS:


I found some articles on internet that stated that I can gzip the website by simply adding code to .HTaccess file. .Htaccess file is a simple file that controls how our website is displayed on the web browsers. Well, I am not a technical person and do not have the expertise to discuss that topic so I will just come to the point.



Enabling Gzip compression on website by .Htaccess:


You just have to paste this code in the .htaccess file to make it work.



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

However, for me, the gzip compression method of placing code in .htaccess file did not work!


So, I was searching for another method to enable Gzip compression on the website. And here is what I found.



Enable Gzip Compression By Index.php:


I found an article on a website that states that we can add php code to index.php page to make your website file size gzip compressed.


So, following the method on that website, here is what I did.


Locate your Index.php file on the server. (this will work for both, wordpress websites and other dynamic websites). After locating the website you just have to add the following text on top of the page.



<? ob_start("ob_gzhandler"); ?>

That is locate the "<?php" string and add the code on top of it.


After placing the code, save your website and refresh it. Now, go to any gzip compression level tester and check your website. Your website will have enabled Gzip compression.


No comments:

Post a Comment