【问题标题】:Isapi Rewrite - access query string pattern matches in RewriteRuleIsapi Rewrite - 在 RewriteRule 中访问查询字符串模式匹配
【发布时间】:2010-11-08 17:14:54
【问题描述】:

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

【问题讨论】:

  • 但是规则被应用了?
  • 是的,没错。我最终在 /test/index.cfm?page=home
  • 那么/test/home.cfm?page=default的初始请求呢?还是您一直在测试/test/index.cfm?page=home?如果是这样,您可能误解了 URL 重写的工作原理。他们只是重写请求的 URI!
  • 糟糕!我刚刚包括了重定向后的日志记录。现已添加完整的日志详细信息

标签: mod-rewrite query-string isapi-rewrite


【解决方案1】:

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

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

日志中没有RewriteCond处理可能是因为RewriteLogLevel低。

配置应该是:

RewriteCond %{QUERY_STRING} ^page=default(.+)$ [NC]

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

【讨论】:

  • 很棒的东西-谢谢伙计!看起来我设置的 [I] 标志导致整个 RewriteCond 被忽略。 [I] - 对于“忽略大小写” - 是 IsapiRewrite 版本 2 的遗留问题。
【解决方案2】:

我认为 ISAPI Rewrite 与 mod_rewrite 有点不同:

每当您在复杂规则(带有条件的规则)中存在的任何正则表达式中放置括号时,您就是在标记可以在格式字符串中使用的子匹配(使用$N 语法)或作为反向引用(使用\N 语法)在后续条件中。这些子数学对于整个复杂规则是全局的(RewriteRule 指令和相应的RewriteCond 指令)。子匹配从与 RewriteRule 对应的第一个 RewriteCond 指令(如果存在这样的指令)开始从上到下和从左到右编号。

所以尝试$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。

【讨论】:

  • 恐怕运气不好。我实际上正在使用 ISAPI Rewrite 3 - 昨天刚刚切换到它 - 这就是我重新制定这些规则的原因
  • 那么我不知道为什么你的不起作用。或者是否有任何其他规则可能与该规则发生冲突?
  • 我已经删除了除 RewriteLog 指令之外的所有其他规则 - 仍然没有成功。我已经编辑了上面的问题以包含日志输出 - 希望这可以阐明一些问题
【解决方案3】:

我相信这也有效:

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

【讨论】:

  • 这种规则在 ISAPI Rewrite 2 中有效——但 ISAPI Rewrite 3 是 Apache mod rewrite 的克隆。来自 mod rewrite 站点:注意:查询字符串 Pattern 不会与查询字符串匹配。相反,您必须使用带有 %{QUERY_STRING} 变量的 RewriteCond。
猜你喜欢
  • 2012-04-14
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
相关资源
最近更新 更多