【发布时间】:2022-01-11 10:30:50
【问题描述】:
我正在尝试在我的 apache 实例 (Apache/2.4.38 (Debian)) 中设置压缩,以便仅在 Request_URI 与一个 api 端点匹配时压缩响应,例如
我想启用压缩:
-
/api/endpoint/123, /api/endpoint/456
但不是为了
-
api/endpoint2/12, -
api/endpoint/123/action, api/endpoint/test
我确定 mod_deflate 已启用。我的想法是首先使用 no-gzip 变量禁用 gzip,然后在 URI 与正则表达式匹配时删除该变量。我的完整 .htaccess 如下所示:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnv no-gzip 1
SetEnvIf Request_URI "/api/endpoint/\d+$" !no-gzip
</IfModule>
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
另一个想法是添加一个新的 RewriteRule(在最后一个之前):
RewriteCond %{REQUEST_URI} !^/api/endpoint/\d+$
RewriteRule ^ - [E=no-gzip:1]
不幸的是,这些工作都没有,也没有响应被压缩......
【问题讨论】:
-
将其包装成一个
IF,检查请求的路径是否匹配两个...? httpd.apache.org/docs/2.4/expr.html#examples