首页 文章

Isapi Rewrite - 在RewriteRule中访问查询字符串模式匹配

提问于
浏览
1

我正在使用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模式检查?

3 回答

  • 3

    但是额外的查询字符串变量永远不会传递 . %1似乎是空白的 .

    %1为空白,因为根据日志,您提出的请求是/test/home.cfm?page=default - 没有第二个参数 .

    日志中缺少RewriteCond处理可能是由于RewriteLogLevel较低 .

    配置应该是:

    RewriteCond% ^ page = default( . )$ [NC]

    RewriteRule ^ / test / home.cfm $ /test/index.cfm?page=home%1 [NC,R = 301,L]

  • -1

    我认为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更兼容 .

  • 0

    我相信这也有效:

    RewriteRule ^/test/home.cfm\?page=default(.*)$ /test/index.cfm?page=home%1 [I,R=301]
    

相关问题