无法在apache http服务器的Location标签中使用上下文路径删除url中的空参数如果没有上下文路径设置,空参数将按预期正确删除,
下面是上下文路径的示例,其中空参数没有被删除(问题语句)
<Location /reports/>
LogLevel alert rewrite:trace8
RewriteEngine On
RewriteBase /
ProxyPass http://1.2.3.4:3000/
ProxyPassReverse http://1.2.3.4:3000/
RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^/reports$ /reports/?%1%2 [R=302,NE,L]
</Location>
下面是没有上下文路径的示例,其中空参数按预期删除(但最终解决方案需要使用上下文路径)
<Location />
ProxyPass http://1.2.3.4:3000/
ProxyPassReverse http://1.2.3.4:3000/
RewriteEngine On
AllowOverride All
RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,NE,L]
</Location>
如何从URL中删除空参数,并在位置中设置上下文路径?
<Location /reports/> : RewriteEngine On RewriteBase / : RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$ RewriteRule ^/reports$ /reports/?%1%2 [R=302,NE,L] </Location>
这里有几个问题…
-
您的
<Location>
块匹配/reports/
(带有尾斜杠),但是您的RewriteRule
模式^/reports$
省略了尾斜杠,因此它永远不会匹配。 -
因此,为了匹配请求,<Location>
块合并很晚,在这种背景下RewriteRule
模式匹配绝对文件系统路径,而不是root-relative url路径。RewriteRule
模式应该像/reports/$
一样,以允许绝对文件系统路径,例如。/path/to/document-root/reports/
。换句话说:RewriteRule /reports/$ /reports/?%1%2 [R=302,NE,L]
或者,完全省略
RewriteRule
模式的路径组件(例如:^
),除非你必须只匹配/reports/
而不匹配/reports/<anything>
。 -
然而,你不应该在
<Location>
块中使用mod_rewrite指令。来自Apache 2.4文档中的RewriteRule
指令:尽管重写规则在
<Location>
和<Files>
节(包括它们的正则表达式对应部分)中在语法上是允许的,但是这不应该是必要的,并且是不支持的。在这些上下文中,一个可能被破坏的特性是相对替换。 -
这里没有使用
RewriteBase
指令。(但在目录上下文之外不支持/不需要)
RewriteRule
模式和<Location>
指令执行相同的检查。将这些mod_rewrite指令移出<Location>
容器,并将它们直接放在<VirtualHost>
容器(或server上下文中)中。例如:
LogLevel alert rewrite:trace8
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^/reports/$ /reports/?%1%2 [R=302,NE,L]
<Location /reports/>
ProxyPass http://1.2.3.4:3000/
ProxyPassReverse http://1.2.3.4:3000/
</Location>