Today I saw a tweet by my friend Niyas that Host Gator does not support mod_gzip. This made me wonder whether it is possible to achieve this using PHP. I contacted HostGator support and confirmed that GZip support for PHP is enabled on both shared and dedicated hosting accounts, whereas mod_deflate Apache module is available only for dedicated hosting customers. Fortunately, we can use PHP to compress files on the fly.
Enabling gzip compression in an existing PHP file is very simple. Add the following line at the beginning of the file (it should appear before any output statement).
<?php
ob_start ("ob_gzhandler");
?>
Very simple, right? But what if we have hundreds of files to edit? Don’t worry, there is solution for that too. We can use the PHP configuration variable auto_prepend_file
to automatically include the above code at the beginning of all PHP files. Copy the above code to a file named prepend.php and place it at the root of your website (~/public_html in CPanel, ~/httpdocs in Plesk).
Now we are going to automatically include the above file at the beginning of all PHP files using auto_prepend_file
. Depending on how PHP is configured on your server, the steps for modifying the PHP configuration variable is also different.
If PHP is configured in CGI/FastCGI mode (HostGator uses this mode), we will be using php.ini file to make the above change. Create a file named php.ini at the root of your website and copy the following code into it. If the file already exists, append it to the end of the file.
auto_prepend_file=<full-path-to-document-root>/prepend.php
If PHP is loaded as apache module (mod_php), we will have to use .htaccess to achieve the same effect. Create a file named .htaccess at the web root and copy the following code into it. If the file exists, append at the end of the file.
php_value auto_prepend_file <full-path-to-document-root>/prepend.php
In both methods, replace <full-path-to-document-root> with the full path to your website root directory, for example /home/joyce/public_html.
Now the prepend.php file is automatically included by the PHP interpreter, whenever a PHP file is requested. But this method (as of now) does not work for non PHP files like HTML, CSS, JavaScript etc, which should also be compressed. This is because these files are handled directly by Apache and is not passed to the PHP interpreter. To ensure that these files are also compressed, we have to instruct Apache to pass these files to the PHP interpreter before sending them to the browser. For this, lets once again go back to .htaccess. Append the following code at the end of your .htaccess file (create one, if you don’t have it already).
<FilesMatch "\.(htm|html|css|js)">
ForceType application/x-httpd-php
</FilesMatch>
The above code instructs Apache that files ending with .js, .htm, .html, .css and .js extensions are also PHP files and should be passed through PHP interpreter. The only problem remaining is that Content-type header for all files have been changed to text/html. To fix this we need to check the requested file-name and set the correct Content-type header depending on the file extension. In order to do this, open prepend.php and replace the current code with
<?php
// Start output buffering
ob_start ("ob_gzhandler");
// Set the correct content type header depending on filename
$arContentType = array('htm' => 'text/html', 'html' => 'text/html', 'css' => 'text/css', 'js' => 'text/javascript');
if(strpos($_SERVER['REQUEST_URI'], '.') !== FALSE){
$ext = explode('.', $_SERVER['REQUEST_URI']);
$ext = array_pop($ext);
if(($pos = strpos($ext, '?')) !== FALSE){
$ext = substr($ext, 0, $pos);
}
if(isset($arContentType[$ext])){
header ("Content-type: {$arContentType[$ext]}; charset: UTF-8");
}
}
// You can also set expiration headers in this file
?>
That’s all Folks. You have successfully enabled Gzip compression on your server without using any Apache modules.
This is a working code. But I have tested it only with mod_php configuration. You have to be very careful when enabling PHP code in JS/CSS files. There may be many flaws in this code. If you find any, please let me know. We can further improve this code by adding expiration and cache headers to enable client side caching.
Wow. Thanks for the wonderful article Joyce!
I am using wp-super-cache plugin in my site. Do you think that I can still use this technique without any problems?
It should work, if you have gzip compression turned off in WordPress/wp-super-cache. Or else it can lead to double compression.
Use Live HTTP Headers to check whether gzip is enabled or not.
If I enable compression from my cPanel and from wordpress caching plugin. Will they conflict or the content will be double compressed?
I am not sure about that. Most probably it will cause double compression.
Joyce thanks for this. Thought your readers might like to know that if they are on shared Hostgator hosting they need to contact support to have the php.ini file uploaded.
Hi!
I’m bumping this, but I just want to say thank you! You have no idea how long I’ve been searching for a solution. I’ve tried at least 10 methods, including plugings, custom javascript et cetera..(running wp with a shared access server, apache)
Anyway, this worked!! Finally!
Can I blog about this (and credit you of course)?
Thanks again,
Andreas
@Andreas – Happy to know that it was useful. Sure, you can use it 🙂