We help IT Professionals succeed at work.

Problems enabling gzip using .htaccess

I have read various guides to setting up gzip compression for my site. I believe I should be able to do it by adding the following to the .htaccess file:

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

I have also tried different versions from:
http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/
http://www.webmaster-talk.com/php-forum/105975-how-to-use-gzip.html

But when I test my site http://www.poems4free.com at http://www.gidnetwork.com/tools/gzip-test.php it returns with no compression.

My hosting provider say that gzip is enabled on the server. The server details are:

Apache version 2.2.13 (Unix)
PHP version 5.2.10
Linux

Any help gratefully received.
T Morphy
Comment
Watch Question

Commented:
Afaik. mod_gzip is legacy product, at least with apache2.2. You should go for mod_deflate

Author

Commented:
Ok so I have also tried the code below with no joy:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>

Commented:
Code is correct. Propably you're missing AllowOverride directive in config relating to your site.
I tested with AllowOverride All and it worked immediately.

You may check if compression is enabled using following site:
http://www.whatsmyip.org/http_compression/

Author

Commented:
Hi thanks for your suggestion.

I asked my hosting provider about this config setting and they replied with a php file set up like this:

<?php
ob_start("ob_gzhandler");

print("<HTML><BODY><br>You are checking a gzip'ed web page.</BODY></HTML>");

ob_flush();
?>

My pages are HTML not PHP. Can i set up gzip for HTML and if so do I have to do something in addition to the code I added to .htaccess?

Author

Commented:
More information received from my hosting provider:

Yes, you will need to "call" the gzip on the content in your files. There is no need to change all of the file content, you can only embed the ob_start("ob_gzhandler"); and ob_flush(); function handlers in the top and bottom of your files (don't forget to rename to .php, or add the line in .htaccess to treat .html files as .php) and it should work for you without any issues.

Unfortunately AllowOverride setting cannot be changed at the moment for security reasons.

I guess that is that. Unless anyone has any other suggestions?

Thanks
T
Commented:
It's pretty weird, they instruct you to use AddType to treat php as html and at the same time that requires server to have AllowOverride Fileinfo. The same directive is required to define SetOutputFilter above.
It is possible to set directives directory based so i see no security related point here why they wouldn't explicitily define that specific directive in your web-directory.
ISP's have their policies.
It might be that they have already set up server wide directive to parse html files as php files. If you add the code to your page like this:
<?php
ob_start("ob_gzhandler");
?>

YOUR HTML PAGE CODE...

<?php
ob_flush();
?>

does it do the job?

Author

Commented:
Hi Aarop,

Thanks for your comments. I have been finding their responses pretty confusing, but it doesn't matter your suggestion works!

Thank you very much for your help.
Tamzin