Bandwidth control is an important task to be followed when your traffic starts increasing. With limited monthly bandwidth hosting, your site may run out of bandwidth and thus result in increase in down time. Hence it is very much advisable to compress and optimise your site with GZIP compression.
Apply GZIP compression and then serve it to the browser client. Compressing output can significantly improve your websites performance by reducing the output size, sometimes by upto 80%!
So how can you enable GZip compression and compress your websites output? Well there are several ways to achieve this. Listed below are some very useful tricks to enable GZip compression.
GZIP Compression using mod_gzip, mod_deflate and htaccess
The Apache server supports server level GZip compression with the help of a module mod_gzip and mod_deflate. You can use this module and enable GZip compression for your website using a htaccess file.
First you have to check whether your hosting provider has enabled mod_gzip or mod_deflate module or not? To check this, you can use php_info()
function of PHP which prints out all the information about server and modules.
Add the following code into a blank file:
1 2 3 4 5 6 | <?php // Show all information, defaults to INFO_ALL phpinfo(); ?> |
Save the file as a .php file using a memorable name. For example: myphpinfo.php. Then upload this to your hosting account.
Once thats done simply visit your SSL site URL i.e. mydomain.com/myphpinfo.php. This will show all the modules available in your hosting environment.
Search for the modules mod_gzip or mod_deflate. If you find them it means they are enabled for your website. If not you may need to get in touch with support to have them activated for you.
Once you have confirmed that mod_gzip or mod_deflate is available, you can then enable compression for text and HTML by adding following lines in your htaccess file.
1 2 3 4 5 6 7 8 9 | # compress all text and html: AddOutputFilterByType DEFLATE text/html text/plain text/xml # Or, compress certain file types by extension: # Or, compress certain file types by extension: <Files *.html> SetOutputFilter DEFLATE </Files> |
You can compress all types of content (images, css, js etc) using mod_deflate. Copy the following code into your htaccess file to do this.
1 2 3 4 5 6 7 | <Location /> SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI \ \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \ \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary </Location> |
For mod_gzip, you can use the following code in your htaccess file to enable compression.
1 2 3 4 5 6 7 8 9 10 | <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> |
This technique only works if mod_gzip or mod_deflate modules are loaded in Apache. For one of our clients, these modules were not loaded and their hosting provider refused to load them as they were using a shared hosting account.
Tip: If this applies to you perhaps you should consider moving to a better hosting provider.
We prefer Umbrella Host servers. They are GZip enabled from the outset allowing you to benefit from the tips below. The unlimited web hosting plan also comes with ample bandwidth so you wont be hitting your limits anytime soon.
The following can be another way of enabling compression.
GZip using PHP ob_start() method
If your web hosting provider does not support mod_gzip module, the ob_start()
method can be used to enable compression via your PHP files. For this you need to copy the following line into the top of your PHP file.
Pro Tip: You may want to add this line at the beginning of your header.php file. That way it will get included in every php file of your website.
1 2 3 4 5 6 | <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?> |
The above code will check whether your browser supports gzip, if yes, then it sends the ob_gzhandler method as a handle to the ob_start method which buffers the output. Thus output is compressed using the ob_gzhandler.
Only problem with this method is that you have to manually edit all PHP files. If you have a header.php file then use that as the code will get included in all files.
If the above methods don’t work for you or are not an option then there are still ways to achieve this without touching your PHP files. Read following tricks.
GZip using php_value directive via htaccess
A php_value directive can be used to append/prepend any PHP files in the request to change the output handler.
First we will see how we can prepend to a PHP file and achieve this.
Copy the PHP code that we saw in the above example into a file called gzip-enable.php and upload it to your hosting account. Now copy following lines into your htaccess file.
1 2 3 4 5 6 | <FilesMatch "\.(txt|html|htm|php)"> ForceType application/x-httpd-php php_value auto_prepend_file /the/full/path/gzip-enable.php </FilesMatch> |
This way you do not to modify any of your PHP files. This trick will prepend a PHP file with the ob_start() method to all the files of your site.
But what if you don’t want to prepend a PHP file? There is still a way to specify default output handler using htaccess.
Use following line in your htaccess file to tell the Apache server to register the ob_gzhandler handler function as an output handler.
1 | php_value output_handler ob_gzhandler |
Compress CSS using htaccess and php_value
Generally CSS stylesheet files occupy a significant size in the overall web page size. It’s advisable to compress these files too, before sending them to client’s browser.
This will significantly improve the performance of a web page. For compressing CSS files, we will first create a PHP file gzip-css.php with following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php // initialize ob_gzhandler function to send and compress data ob_start ("ob_gzhandler"); // send the requisite header information and character set header ("content-type: text/css; charset: UTF-8"); // check cached credentials and reprocess accordingly header ("cache-control: must-revalidate"); // set variable for duration of cached content $offset = 60 * 60; // set variable specifying format of expiration header $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT"; // send cache expiration header to the client browser header ($expire); ?> |
Now add following lines in your htaccess file to enable compression for your CSS files.
1 2 3 4 5 6 | <FilesMatch "\.(css)"> ForceType application/x-httpd-php php_value auto_prepend_file "/the/full/path/of/this/file/gzip-css.php" </FilesMatch> |
Whenever a http request for a .css comes to your server, this type of css will get converted into application/x-httpd-php, thus parsing them using PHP.
Then a file gzip-css.php will be perpend to this request which in turn compresses the output using ob_start (“ob_gzhandler”) method.
After adding above code into your files, you can check that gzip compression is enabled or not by using the following site:https://www.whatsmyip.org/http_compression/
We use Gzip compression on our blog and make some great savings, see for yourself: Valen Digital’s Gzip Test
Have we missed anything out? Let us know below OR if you have a better way of using GZip by leaving us a comment.