我正在使用Isapi Rewrite 3(IIS的mod重写克隆)并尝试根据查询字符串重写URL - 然后在重写中传递部分查询字符串。
因此,如果我输入这样的网址: /test/home.cfm?page=default&arbitraryExtraArg=123
我希望将其重写为: /test/index.cfm?page=home&arbitraryExtraArg=123
我有以下条件/规则:
RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [I,R=301]
但是永远不会传递额外的查询字符串变量。 %1似乎是空白的。
这是如何引用RewriteCond的模式匹配,对吗?
我在这里缺少什么?
谢谢!
编辑:在我看来RewriteCond被完全忽略了。这是我的日志文件的样子:[snip] (2) init rewrite engine with requested uri /test/home.cfm?page=default
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/home.cfm'
[snip] (1) escaping /test/index.cfm?page=home
[snip] (2) explicitly forcing redirect with http://www.devsite.com/test/index.cfm?page=home
[snip] (2) internal redirect with /test/home.cfm?page=default [INTERNAL REDIRECT]
[snip] (2) init rewrite engine with requested uri /test/index.cfm?page=home
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/index.cfm'
那里是否应该提到RewriteCond模式检查?
答案 0 :(得分:3)
但是永远不会传递额外的查询字符串变量。 %1似乎是空白的。
%1为空白,因为根据日志,您提出的请求是/test/home.cfm?page=default - 没有第二个参数。
日志中缺少RewriteCond处理可能是由于RewriteLogLevel低。
配置应该是:
RewriteCond%{QUERY_STRING} ^ page = default(。+)$ [NC]
RewriteRule ^ / test / home.cfm $ /test/index.cfm?page=home%1 [NC,R = 301,L]
答案 1 :(得分:0)
我认为ISAPI Rewrite与mod_rewrite有点不同:
每当您将括号放在复杂规则(具有条件的规则)中的任何正则表达式中时,您标记的子匹配可以在格式字符串中使用(使用
$N
语法)或作为反向引用(在后续条件中使用\N
语法)。这些子对于整个复杂规则( RewriteRule 指令和相应的 RewriteCond 指令)是全局的。从第一个 RewriteCond 指令(如果存在这样的指令)开始,子匹配从上到下,从左到右编号,对应于 RewriteRule 。
因此,请尝试$1
以获得第一组的匹配,无论它出现在何处:
RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home\.cfm$ /test/index.cfm?page=home$1 [I,R=301]
ISAPI Rewrite 3似乎与Apache的mod_rewrite更兼容。
答案 2 :(得分:-1)
我相信这也有效:
RewriteRule ^/test/home.cfm\?page=default(.*)$ /test/index.cfm?page=home%1 [I,R=301]