【问题标题】:Can I tell mod_deflate & PHP to skip compression on one directory only?我可以告诉 mod_deflate 和 PHP 只在一个目录上跳过压缩吗?
【发布时间】:2013-01-05 11:07:55
【问题描述】:

为了压缩我的所有网页,我使用了这个 .htaccess 代码。如果可能,它使用 Apache deflate 模块,否则应用 PHP ob_gzhandler 压缩。

一切正常,但出于特定原因,我不想对文件夹 ./folderWithoutCompression 应用压缩。

问题:如果在下面的脚本中定义了 Apache deflate 模块(PHP ob_gzhandler 情况),我如何添加此异常?

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

编辑

作为解决方法,我对文件夹WithoutCompression 文件夹中的所有文件使用自定义content-type

我确信可以使用&lt;Location&gt;&lt;LocationMatch>&lt;DirectoryMatch>&lt;Directory> 排除某些目录,但是当我尝试处理时,总是出现 Apache 错误 500

【问题讨论】:

  • 或许不可能?
  • 我终于在这里找到了答案:stackoverflow.com/questions/1922934/…Thanks @Gumbo
  • 我赞成你的问题,因为它为我提供了答案:以前从未听说过这个过滤器模块,谢谢!
  • 我知道很久以前有人问过这个问题。正确的方法是使用“RemoveOutputFilter DEFLATE”。查看 RemoveOutputFilter 的文档,它特别适用于带有 .htaccess 文件的子目录。

标签: php apache .htaccess mod-deflate


【解决方案1】:

我终于在这里找到了答案:

How to disable mod_deflate in apache2?

谢谢@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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2012-06-22
    相关资源
    最近更新 更多