【问题标题】:Using variable in RewriteCond condition patern在 RewriteCond 条件模式中使用变量
【发布时间】:2014-06-20 12:20:20
【问题描述】:

我想将用户 ID 与特定应用 ID 相关联,例如:

<user_id> <app_id>  
615 1
616 7
617 3
618 3    

我的 URI 看起来像:

/<app_id>/<user_id>/...

现在,我希望能够在不影响用户书签的情况下轻松更改应用程序。在我的例子中,我想要两个

/1/615/index.html or /3/615/index.html

担任

/1/615/index.html

通过以下规则,我得到无限循环:

RewriteMap map dbm:user-application.map
RewriteRule ^/([0-9]+)/([0-9]+)/(.*)$ /${map:$2}/$2/$3 [R,L]              
...
#other proxy code to forward request to applications

我了解重定向后,Apache 将始终执行相同的规则。 然后我尝试添加一个重写条件来阻止循环,比如

RewriteMap map dbm:user-application.map
RewriteCond %{REQUEST_URI} !^/${map:$2}
RewriteRule ^/([0-9]+)/([0-9]+)/(.*)$ /${map:$2}/$2/$3 [R,L]              

如果我正确阅读了我的重写日志,我可以看到变量 !^/${map:$2} 没有在条件模式中被替换,而是“按原样”检查。然后条件总是为真,我仍然得到我的无限循环。

应用程序 ID 与我的地图匹配后立即阻止循环的任何想法?

【问题讨论】:

标签: apache mod-rewrite rewritemap


【解决方案1】:

/3/615/index.html 正确重定向到 /1/615/index.html

问题是您也将 /1/615/index.html 重定向到 /1/615/index.html - 您想检测地图转换是无操作和 的情况在这种情况下根本不重定向。

如果您不关心面向用户的 URL,只需将 [R,L] 更改为 [L](删除 R)就可以了,因为它不会触发新的往返客户。

$2 反向引用在 RewriteCond 表达式中不起作用是对的;这是因为尚未评估 RewriteRule。您也许可以在以前的 RewriteCond 中使用 %n - 样式反向引用到正则表达式...

RewriteCond {%REQUEST_URI} ^/([0-9]+)/
RewriteCond {%REQUEST_URI} !^/${map:%1}

但是我没有测试过这个,所以YMMV。

【讨论】:

  • 好吧,我无法提供我使用的确切修复方法,但您提供的答案确实是我探索的方式。谢谢!
  • 这篇博文讲述了一个极其相似的案例:jeremytunnell.com/posts/…
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2020-10-30
  • 2023-03-21
  • 2013-03-25
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多