Compression – Part III: .js and .css files

13 04 2010

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:

<IfModule mod_php5.c>
php_flag output_buffering on
php_value output_handler 'ob_gzhandler'
# compress all js and css files
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE

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.

This work is licensed under a Creative Commons License



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: