- SEO - Search Engine Optimization
- A Guide to the Robots.txt Exclusion Protocol
- What are XML sitemaps?
- Using Google Webmaster Tools
- Getting Started with Google Analytics
- Getting Started Earning Money with Adsense
- Website Loading Times Are Vital - How to Improve Yours
- Improve Website Speed by Enabling Compression
- Google Trends Keyword Comparison Tool
- 8 Excellent (and Free!) Search Engine Optimization Websites
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.
If 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 .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 mod_gzip
is not available, you can use the alternative mod_deflate
which generally comes along with the apache package.
To enable mod_deflate
simple add these lines to your .htaccess
file.
<ifmodule mod_deflate.c> AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript </ifmodule>
GZip Compression with PHP
PHP can easily compress generated content before sending it to the browser and works in the same way as I described a few years ago when I wrote about compressing JavaScript and CSS files.
The preferred method for enabling compression in PHP is to set the zlib.output_compression
variable in php.ini
Name | Default | Changeable | Changelog |
---|---|---|---|
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.
<?php ini_set("zlib.output_compression", "On"); ?>
Another option you can use is to use PHP's output buffering functions.
<?php
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';
}
else
{
$encoding = false;
}
if($encoding)
{
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("x1fx8bx08x00x00x00x00x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
}
else
{
ob_end_flush();
exit();
}
}
ob_start();
ob_implicit_flush(0);
// Do your PHP awesomeness here
echo 'Hello World';
// finally, call this function to send the compressed page
print_gzipped_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.
This post is part of the series Search Engine Optimisation. Use the links below to advance to the next tutorial in the couse, or go back and see the previous in the tutorial series.