Improve Website Speed by Enabling Compression
- Why Website Loading Times Are Vital - How to Improve Yours
- Improve Website Speed by Enabling Compression
- Timing PHP Script Execution
There are two ways that you can improve your website speed by enabling compression for your generated content.
The first method works for static content, such as HTML and CSS, while the second only works on pages served generated with PHP.
htaccess GZip Compression
The simplest method for enabling GZip compression uses the Apache module
mod_gzip which is available on most hosting platforms.
Mod_gzip allows you to use the Gzip compression method to create a significant reduction in the volume of web page content served over the HTTP protocol.
mod_gzip has not been installed on your server, and you have access to install modules, you can use this tutorial to install mod_gzip.
Once installed, enabling GZip compression is as easy as adding a few lines to the
- <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_exclude mime ^image/.*
- mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
mod_gzip is not available, you can use the alternative
mod_deflate which generally comes along with the apache package.
mod_deflate simple add these lines to your
- <ifmodule mod_deflate.c>
GZip Compression with PHP
The preferred method for enabling compression in PHP is to set the
zlib.output_compression variable in
|zlib.output_compression||"0"||PHP_INI_ALL||Available since PHP 4.0.5.|
|zlib.output_compression_level||"-1"||PHP_INI_ALL||Available since PHP 4.3.0.|
|zlib.output_handler||""||PHP_INI_ALL||Available since PHP 4.3.0.|
If you cant update your php.ini configuration file, you could try using
ini_set php function.
- ini_set("zlib.output_compression", "On");
Another option you can use is to use PHP's output buffering functions.
- function print_gzipped_page()
- global $HTTP_ACCEPT_ENCODING;
- if( headers_sent() )
- $encoding = false;
- else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
- $encoding = 'x-gzip';
- else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
- $encoding = 'gzip';
- $encoding = false;
- $contents = ob_get_contents();
- header('Content-Encoding: '.$encoding);
- $size = strlen($contents);
- $contents = gzcompress($contents, 9);
- $contents = substr($contents, 0, $size);
- // Do your PHP awesomeness here
- echo 'Hello World';
- // finally, call this function to send the compressed page
Using either one of these methods should result in faster website download speed, but you still need to have an optimised high-performance website, to begin with. If your page takes 10 seconds to generate, all the Gzipping in the world isn't going to improve site performance.
Once implemented, you should test your GZip compression to ensure that it working correctly. There are numerous sites that can do this for you.
Last updated: 2017-09-08