0

A strange problem occurs when GZipping files on our (updated?migrated) shared hosting server.

Examine these two identical files in the the includes folder /_inc/ with exactly the same contents (php code) with the purpose of collecting, minifying and generating a single Gzipped css file from various source cdd files. Now, files ending with php seem to get GZipped nicely, however files ending with extension css and js do not get GZipped yet...

/_inc/all.css (24 kbyte, extension won't let herself Gzipped)
/_inc/allcss.php (5 kbyte! extension lets herself Gzipped)

Changing the extension from css to php makes the file to compress as you see, BUT browsers still prefer the css extension for best caching...

Q: Why is it, that only files ending with php are Gzipped, and php files ending with css and js extension are not Gzipped? How to achieve Gzipping for css and js extensions as well? How can I set the main .htacces or php.ini to Gzip all .php, .css, .js files as well?


/.htaccess (root .htaccess file portion for compression)

## interpreted extensions as php
AddType application/x-httpd-php .php
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
#AddType text/css css
#AddType text/javascript js

## compress speficic filetypes BUTTTT doesn't work anymore since server upgrade!
## with or without the code below the result is the same!
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|eot|ttf|svg|xml|php|txt)$">
    SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

/_inc/.htaccess (.htacces file that makes css and js files interpret as php files)

AddType application/x-httpd-php-cgi .js .css
Action application/x-httpd-php-cgi /cgi-php/php-cgi

php.ini (from the Apache server running PHP 5.3.28)

session.gc_maxlifetime = 1440
session.name = PHPSESSID
session.save_handler = files
session.serialize_handler = php
zlib.output_compression = On
zlib.output_handler = ob_gzhandler
allow_call_time_pass_reference = 
implicit_flush = Off
Sam
  • 15,254
  • 25
  • 90
  • 145
  • Am I missing something? You have explicitly commented out the part of your config that enables compression. And now instead of your web server compressing your PHP output, you're letting PHP do it. Also, the browsers do not care one bit about file name extensions, and do not change caching behavior based on file name. – Brad Sep 01 '14 at 23:17
  • Dear @Brad, thanks for your clear comment. Firstly the commented apache rules seem to have NO effect at all on or off the result is the same, namely that only php files are compressed. Onyour second remark: intersting! That would mean that I am wrong and that .css and .js files can JUST ASWELL be called css.php and js.php and it all works and caches everywhere the same way as native .css and .js files?? is this really true?? – Sam Sep 02 '14 at 00:05
  • I'm not sure why your mod_deflate config isn't working. But yes, file name extensions are meaningless when it comes to HTTP. For content type, only the `Content-Type` response header is meaningful. For caching, only the caching headers are meaningful, such as `Cache-Control` and `Expires`. – Brad Sep 02 '14 at 01:20
  • @Brad thanks for new insights... Yes previously it did work untill some ugrade on the shared hosting, then compression suddenly stopped working and only by adding the lines `zlib.output_compression = On` and ` zlib.output_handler = ob_gzhandler` in the php.ini it works again for php files only now. The only problem with making all files php is that I can't set expiration for them on apache. I must then do that within the css and js files but perhaps php headers work just as good as general apache headers? – Sam Sep 02 '14 at 02:17
  • Headers are headers. If you output a response header with cache control, it doesn't matter whether it came from PHP or Apache... the client doesn't know or care. As for mod_deflate, I recommend letting Apache do the compression rather than PHP. At least for Nginx, the web server doing the compression is slightly faster than having PHP do it. And then, you get more control over **what** you compress. (For instance, I have a situation where I output JPEGs from PHP. I don't want those to be compressed since there is little benefit and large CPU overhead.) – Brad Sep 03 '14 at 13:09

0 Answers0