Compress Your Web Page
General information about compressing your web pages
Many people agree that :
- speed gains are negligible,
- it reduces bandwidth… I like!
- it does not effect images (gifs / jpgs / pngs) since they’re already compressed
- you can (add it to and) use .htaccess to make it work on your entire web site alternatively.
In my never-ending quest to get my web pages to load super-fast I chanced upon ob_gzhandler() one day while researching the uses of ob_start() in PHP.
What it does is compress your markup/HTML (web page) every time it is requested by a browser capable of handling compressed web pages.
As usual, I will not get too technical on how it works but instead show you how to use it on your web pages like I have. And believe me, it’s so simple you can even add it to your existing pages without too much effort.
If it’s not already obvious, you must be certain that you use PHP for your web pages and that your server supports it at least.
About PHP on your server
Then, you have to be sure that the version of PHP on your web server is extended with the zlib module. To find out quickly, just open a text editor and type in the following:
<?php phpinfo(); ?>
Then save the file as phpinfo.php and upload it to your web site. Once you view this page with your browser, look for the zlib section on the web page. If it’s installed, one table on the page would look like this:
On that same page, just note that the PHP version on your web server is 4.0.6 or higher, otherwise you can’t use this method to compress your web pages.
Sample code to compress your web pages
To compress a web page, this is basically how you would do it:
<?php ob_start( 'ob_gzhandler' ); ?> <html> <head> </head> <body> <p>This web page is now COMPRESSED!</p> </body> </html>
Another way to compress PHP-driven web pages
here’s one more: Compress your web pages using ini_set('zlib.output_compression', '1').
Enable compression on your entire web site
With an Apache Web Server, it’s very easy. All you do is identify the types of files you want to serve compressed, insert the list to your server configuration file, and reload your web server.
Typical types of files you would want to serve compressed every time include: HTML CSS Java Script Plain Text XML
You should also consider other “text” file types, like .json files, for example, if you use or serve them extensively on your site. You do not want your web server to compress files that are already compressed, such as popular media files and archive files. Typical media files like video, audio and image files are usually already compressed. In other words, if they exist with file extensions like .mp4, .png, .jpg, .mp3 etc., then they are already in some form of a compressed state. Common (compressed) archive files include those that have .zip, .gz, .bz2, .rar, and .7z extensions.
Note : WordPress does not have a setting anywhere where you can enable compression for your web site. The instructions in this page is recommended, if you want your self-hosted WordPress-powered web site to run optimally.
Apache Web Server
If your web server is Apache, and if the mod_deflate module is enabled on it, the following method is the easiest way to get compression enabled for your entire web site. You just insert something like this to the .htaccess file in your web document root folder:
About Apache module mod_deflate.
Or, if you want this to only apply to a particular directory (and its sub-directories), and you are able to add/modify httpd directives to a <VirtualHost> container yourself – where the directory is /usr/local/httpd/htdocs: