【问题标题】:How do I disable GZip with SetEnvIfNoCase in Apache .htaccess?如何在 Apache .htaccess 中使用 SetEnvIfNoCase 禁用 GZip?
【发布时间】:2016-07-12 16:35:44
【问题描述】:

我想为某些页面禁用 GZip。我的.htaccess 中有这个,但在访问dashboard/index 时它仍然会打开 GZip (Content-Encoding: gzip)。

<ifmodule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  SetEnvIfNoCase Request_URI /dashboard/index no-gzip dont-vary

我尝试添加 Header set MyHeader %{REQUEST_URI} 以查看 Request_URI 是什么,但它给出了内部服务器错误。

我还尝试了正则表达式dashboard/indexdashboard/index.*"/dashboard/index" 等,并尝试了SetEnvIfNoCase REQUEST_URI ...,但 GZip 仍然打开。

如果我评论 #AddOutputFilterByType,则 GZip 将被关闭。

我正在使用 Apache 2.4.16、Yii 2.0.7、PHP。我在生产中使用 FPM,所以apache_setenv() 不可用。

【问题讨论】:

  • 我能够使用 Header set myHeaderName "%{REQUEST_URI}e" 输出带有请求 URI 的标头,这会产生标头 myHeaderName: /project/web/dashboard/index。 GZip 仍处于开启状态。

标签: apache .htaccess gzip setenvif


【解决方案1】:

您可能正在使用重写来删除 URL 中的 index.php。由于SetEnvIf 在请求期间运行的阶段,index.php 将成为使用的Request_URI var 的一部分(与%{REQUEST_URI} 不同)。

现在很常见的是不使用PATH_INFO 进行重写,而只是简单地重写为index.php,其中代码只是读取原始REQUEST_URI 信息。在这种情况下,SetEnvIf 中的Request_URI 将只是“index.php”,因此您需要在该 URL 的特殊虚拟重写中设置一个标志 env var,并稍后使用 REDIRECT_ 前缀引用它(因为在重写时有一个内部重定向阶段,其中 mod_rewrite 为所有现有的环境变量加上 REDIRECT_ 前缀):

RewriteRule ^dashboard/index - [E=no-gzip:1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
SetEnvIf REDIRECT_no-gzip 1 no-gzip

如果您重写为PATH_INFO,则有一种稍微不那么冗长的方式(因此“/foobar”使用例如RewriteRule (.*) index.php/$1 规则变为“/index.php/foobar”):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]
SetEnvIfNoCase REQUEST_URI ^/index.php/dashboard/index.*$ no-gzip dont-vary

但这似乎更脆弱,因为如果您更改 RewriteRule 机制,它会损坏。

【讨论】:

  • 非常感谢!我一直在努力 16 小时以上,以了解为什么 no-gzip 和 dont-vary 在 .htaccess 中的重定向(为 symfony 和生产环境中构建,因此 URL 的 app.php 部分丢失和发生内部重定向)。 GG!
猜你喜欢
  • 1970-01-01
  • 2014-07-07
  • 2021-06-23
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2014-01-30
  • 2018-05-06
  • 1970-01-01
相关资源
最近更新 更多