Recently I reviewed PHP’s ob_gzhandler() which only pertains to compressing PHP files. So, what can one do about having .css or .js files sent by the web server in a compressed form decompressed by a supporting browser?
I have found that the below straightfoward approach works on Apache 2 if you add the following to its httpd.conf file or else to a .htaccess file:
php_flag output_buffering on
php_value output_handler 'ob_gzhandler'
# compress all js and css files
I tested this on a Windows box with respect to altering the Apache config file as well as subsequently using only a .htaccess file. Then I tested the above directives on a remote server that runs in a shared-hosting Linux environment. In this case, I was only able to use the .htaccess file and it ran perfectly.