GZIP HTML Content on Shared Hosting

If you host your website on a shared Apache hosting account, it’s highly unlikely that you will have access to the httpd.conf file to set default GZIP compression for your website files. A quick and simple alternative is to set GZIP compression on each page so that you can effectively compress the HTML output content on page load. Note: This only works for pages with the .php file extension and only GZIPs the HTML.

The PHP code is placed at the very top of your page, I.e. the very first piece of code to be executed on the page. It is also recommended that the closing PHP tag is on the same line as the HTML doctype for optimal backward compatibility.

// GZIP Compression
ob_start( 'ob_gzhandler' );

The following example shows how to place the PHP GZIP code at the top of your pages. It is always advisable to test this on a non–important page before rolling out across all pages on your site.

// GZIP Compression
ob_start( 'ob_gzhandler' );
?><!DOCTYPE html>
<!-- head content goes here -->
<!-- body content goes here -->