[thelist] HTML Output Compression

Dejan Kozina dejan at kozina.com
Sun Mar 6 14:01:54 CST 2005


I'd go for a simpler solution.
first add this to the .htaccess:
'AddType application/x-httpd-php php .htm .html'
this would send all your HTML files to PHP for processing. As long as 
they do not contain PHP code they'll be sent back to Apache as they are.
Now, if PHP is installed as an Apache module you just add another line 
to .htaccess: 'php_flag zlib.output_compression on'.
This should on transparent Zlib compression on everything that is output 
by PHP, including your HTML-only files.
If this last line causes a 500 Server Error it means that PHP is 
installed as a CGI and you can't control it ffrom .htaccess. This is 
often the case on shared servers, when the admin forces separation 
between users with phpsuexec or something similar. The major part of 
such suexec programs allow you then to configure PHP on a per-directory 
basis putting a php.ini file in each folder. In this case just create a 
php.ini with the line 'zlib.output_compression=On' and you should be OK.

djn


J.J. SOLARI wrote:
> Hello all,
> 
> I have a question dealing with serving compressed (gzip) HTML
> output. The site is mostly about providing translations of some W3C
> Recommendations (see <http://www.yoyodesign.org/doc/w3c/> for an
> idea)
> 
> These are generally heavy weight documents, size of some nearing
> 1Mo, and so are good candidates regarding compression.
> 
> Unfortunately, current provider offers basic hosting services - ie
> almost no (or very limited) modifications via htaccess file -,
> which is a good reason to look elsewhere for better hosting. The
> one I am considering now allows (tentatively) a great deal of
> customization, in particular with respect to Apache htaccess
> directives.
> 
> Compression is only available through PHP (module mod_gzip is
> surprisingly not proposed).
> 
> Borrowing from the Web, here is what I came to. Let site structure
> be:
> 
> /base_dir
> /base_dir/translation1/
> /base_dir/translation2/
> .etc
> 
> A htaccess file (/base_dir/.htaccess) which contains:
> 
> Options +Indexes
> AddHandler compress .html
> Action compress /base_dir/gzip.php
> 
> 
> With associated PHP script (/base_dir/gzip.php):
> 
> <?php
> ob_start( 'ob_gzhandler' );
> $file = basename( $_SERVER['REQUEST_URI'] );
> readfile( $file );
> ?>
> 
> A complete URL must be provided, for example
> <http://www.example.org/base_dir/index.html>, or
> <http://www.example.org/base_dir/translation1/some_file.html>;
> otherwise, if you have <http://www.example.org/base_dir/>, a
> warning is displayed such as:
> 
> readfile(test): failed to open stream: No such file or directory in
> /realpath_to/base_dir/gzip.php on line 4
> 
> 
> Question1: what can be done in htaccess to account for these later
> URLs?
> 
> Getting greedy now :-)
> I would like to keep this transparent compression scheme, that is there is no
> need to incorporate any code in HTML pages for compression to occur, and also
> to optimize performance somehow. From php.net site, compression defaults to 6
> on a scale ranging from 0 to 9, and according to users, optimal value would lie
> between 1 and 6, considering bandwidth/CPU usage ratio.
> 
> Question2: Any suggestion to implement this feature?
> 
> 
> Thanks,
> 
> JJS.

-- 
Dejan Kozina
Dolina 346 (TS) - I-34018 Italy
tel./fax: +39 040 228 436 - cell.: +39 348 7355 225
http://www.kozina.com/  - e-mail: dejan at kozina.com


More information about the thelist mailing list