Can I tell mod_deflate & PHP to skip compression on one directory only?
Asked Answered
H

1

9

To compress all my web page, I use this .htaccess code. It uses Apache deflate module if possible, else apply the PHP ob_gzhandler compression.

Everything is working fine, but for specific reason, I don't want to apply the compression for the folder ./folderWithoutCompression.

Question : How can I add this exception in case of Apache deflate module is defined or not (PHP ob_gzhandler case) in my following script below?

<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*)+|[X~-]{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
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/css
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/plain
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/x-component
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/javascript
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/json
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xhtml+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/rss+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/atom+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/vnd.ms-fontobject
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/svg+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/x-font-ttf
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $font/opentype
    FilterChain     COMPRESS
    FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no
  </IfModule>

  <IfModule !mod_filter.c>
    # Legacy versions of Apache
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml 
    AddOutputFilterByType DEFLATE application/atom+xml
    AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject 
    AddOutputFilterByType DEFLATE application/x-font-ttf font/opentype
  </IfModule>
</IfModule>

<IfModule !mod_deflate.c>
    #Apache deflate module is not defined, active the page compression through PHP ob_gzhandler
    php_flag output_buffering On
    php_value output_handler ob_gzhandler
</IfModule>

EDIT

As workaround, I am using a custom content-type for all files present in my folderWithoutCompression folder.

I am sure that it is possible to exclude some directory using <Location>, <LocationMatch>, <DirectoryMatch> or <Directory>, but when I try to deal with, I always got an Apache error 500

Hydantoin answered 5/1, 2013 at 11:7 Comment(4)
Maybe it is not possible?Hydantoin
I finally find the answer here : #1923434 Thanks @GumboHydantoin
I upvoted your question because it served as an answer for me: never heard of this filter module before, thanks!Boilermaker
I know its a long time ago this was asked. The correct way to do this is to use "RemoveOutputFilter DEFLATE". Check the documentation for RemoveOutputFilter, it's use is especially for subdiretories with .htaccess files.Ipswich
H
0

I finally find the answer here :

How to disable mod_deflate in apache2?

Thanks @Gumbo

# for URL paths that begin with "/foo/bar/"
SetEnvIf Request_URI ^/foo/bar/ no-gzip=1

# for files that end with ".py"
<FilesMatch \.py$>
    SetEnv no-gzip 1
</FilesMatch>
Hydantoin answered 30/4, 2015 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.