1

I'm using .htaccess provided by http://html5boilerplate.com/ which is correctly compressing the .html, .css, & .js files but I have not been able to get the .php files to compress unless I add <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?> to the top of the page. The last thing I want to do is add that line to a hundred plus pages.

I've been using the following two sites to verify the gzip compression:

The following suggestions have not worked either:

I've also tried the following (http://perishablepress.com/fast-effective-php-compression/):

<?php // placed in a gzip_start.php file
ob_start("ob_gzhandler");
?>

<?php // placed in a gzip_stop.php file
ob_flush();
?>

# Placed in the .htaccess filew
# dual file includes for PHP compression
php_value  auto_prepend_file  /specify/full/path/to/gzip_start.php
php_value  auto_append_file   /specify/full/path/to/gzip_stop.php

Here's the Gzip section of my .htaccess file:

# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

<IfModule mod_deflate.c>

# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
  <IfModule mod_headers.c>
    SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s,?\s(gzip|deflate)?|X{4,13}|~{4,13}|-{4,13})$ HAVE_Accept-Encoding
    RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
  </IfModule>
</IfModule>

# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
  FilterDeclare   COMPRESS
  FilterProvider  COMPRESS  DEFLATE resp=Content-Type /text/(html|css|javascript|plain|x(ml|-component))/
  FilterProvider  COMPRESS  DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
  FilterChain     COMPRESS
  FilterProtocol  COMPRESS  change=yes;byteranges=no
</IfModule>

<IfModule !mod_filter.c>
  # Legacy versions of Apache
  AddOutputFilterByType DEFLATE text/text text/html text/plain text/css application/json
  AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript 
  AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
</IfModule>

# Webfonts and SVG:
  <FilesMatch "\.(ttf|otf|eot|svg|php)$" >
    SetOutputFilter DEFLATE
  </FilesMatch>
</IfModule>
4
  • 1
    if you have a hundred plus pages in your project then perhaps you need to rethink your design pattern May 3, 2012 at 19:37
  • What is the Content-Type the server send when you access the php files?
    – Gerben
    May 5, 2012 at 9:52
  • @Gerben Excellent question. Content-Type:text/html; charset=UTF-8 May 7, 2012 at 16:53
  • Did you resolve this? Have you checked the values of zlib.output_compression?
    – MrWhite
    Sep 12, 2012 at 12:24

2 Answers 2

0

Edit Your .htaccess File like This

# 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>

<files *.css>
SetOutputFilter DEFLATE
 </files>

<files *.js >
SetOutputFilter DEFLATE
 </files>

<files *.img >
SetOutputFilter DEFLATE
 </files>
2
  • Just posting code without any context or explanation isn't a sufficient answer. I suggest you read over how to answer and expand it. Dec 14, 2013 at 5:07
  • Image files should never be compressed as they are already compressed. Doing so will only cause CPU Performance degradation and sometimes image corruption as well. Mar 5, 2016 at 7:02
0

If you have views with .php extension. You will have to add this to .htaccess file

<files *.php >
SetOutputFilter DEFLATE
 </files>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.