mod_rewrite问题

时间:2009-02-13 21:19:20

标签: apache mod-rewrite proxy

我想服务/ foo和/ foo / local,但代理请求/ foo / *到远程服务器。但是,以下规则与以上所有内容相匹配。我做错了什么?

RewriteRule ^/foo/(.+)$ http://remote.host/$1 [P,L]

3 个答案:

答案 0 :(得分:1)

您需要为前几个条件转义,以便它们不会将它们全部发送到远程主机。试试这个:

RewriteEngine On
RewriteRule ^foo$ /$1 [L]
RewriteRule ^foo/$ /$1 [L]
RewriteRule ^foo/([a-zA-Z0-9].*)$ http://example.com/$1 [L]

第一条规则检查第一个条件是否为/foo。如果是这样,请留在家里。

接下来的测试检查是否只是/foo/。如果是这样,再次留在当地。

最后一次测试检查斜杠后面是否有任何悬空,如果有,那么你可能想要远程主机并将其发送到那里。

答案 1 :(得分:0)

好吧,因为mod_rewrite通常会从匹配的文本中删除前导斜杠,所以我怀疑你要么不完美地转录/匿名,要么在重写配置中还有很多其他事情要做。由于模式/foo/.+匹配/ foo的不可能性,这似乎进一步证实了这一点。

您可以展开并仔细检查您从重写配置中发布的内容,以便我们了解其他内容可能会发生什么?

答案 2 :(得分:0)

我想我知道了 - 在某处默认的docname被设置为index.php,它默默地被附加到我的重写中。

RewriteLog输出:

(2) init rewrite engine with requested uri /foo
(3) applying pattern '^/foo(/.+)+$' to uri '/foo'
(1) pass through /foo
(2) init rewrite engine with requested uri /foo/
(3) applying pattern '^/foo(/.+)+$' to uri '/foo/'
(1) pass through /foo/
(2) init rewrite engine with requested uri /foo/index.php
(3) applying pattern '^/foo(/.+)+$' to uri '/foo/index.php'
(2) rewrite '/foo/index.php' -> 'http://remote.host//index.php'
(2) forcing proxy-throughput with http://remote.host//index.php
(1) go-ahead with proxy request proxy:http://remote.host//index.php [OK]