Apache mod_rewrite catch all(。*)无法正常工作

时间:2015-07-27 11:49:15

标签: apache mod-rewrite reverse-proxy

以下是我httpd.conf的摘录。我希望在[2]捕获所有不匹配的请求,并将代理转发到http://localhost:8585。但是,所有链接都已发送到http://localhost:8585/var/ww/html/

DocumentRoot "/var/www/html"

<Directory />
    Options FollowSymLinks
    AllowOverride None
    RewriteEngine on
    RewriteRule testing/(.*) http://localhost:8585/mytesting/$1 [P,L] # [1]
    RewriteRule (.*) http://localhost:8585/$1 [P,L] # [2] Catch all Rule

</Directory>

以下是我尝试的一些测试和重写后的结果

好结果

http://localhost/testing/ =&gt; http://localhost:8585/mytesting/ [3] http://localhost/testing/goodOne =&gt; http://localhost:8585/mytesting/goodOne [4]

糟糕的结果

http://localhost:8080/testing =&gt; http://localhost:8585/var/www/html/testing [5]我希望http://localhost:8585/testing

http://localhost:8080/ =&gt; http://localhost:8585/var/www/html/ [6]我希望http://localhost:8585/

问题很明显,重写后的最后RewriteRule前置DocumentRoot(/ var / www / html /)。我做错什么了吗?或者这是预期的行为?我如何修复/解决此问题以获得我的预期结果?

RewriteLogRewriteLogLevel 2

的输出

[3]

::1 - - [{date}] [xxx][yyy] (2) [perdir /] rewrite 'var/www/html/testing/' -> 'http://localhost:8585/mytesting/'
::1 - - [{date}] [xxx][yyy] (2) [perdir /] escaped URI in per-dir context for proxy, http://localhost:8585/mytesting/ -> http://localhost:8585/mytesting/
::1 - - [{date}] [xxx][yyy] (2) [perdir /] forcing proxy-throughput with http://localhost:8585/mytesting/
::1 - - [{date}] [xxx][yyy] (1) [perdir /] go-ahead with proxy request proxy:http://localhost:8585/mytesting/ [OK]

[4]

::1 - - [{date}] [xxx][yyy] (2) [perdir /] rewrite 'var/www/html/testing/goodOne' -> 'http://localhost:8585/mytesting/goodOne'
::1 - - [{date}] [xxx][yyy] (2) [perdir /] escaped URI in per-dir context for proxy, http://localhost:8585/mytesting/goodOne -> http://localhost:8585/mytesting/goodOne
::1 - - [{date}] [xxx][yyy] (2) [perdir /] forcing proxy-throughput with http://localhost:8585/mytesting/goodOne
::1 - - [{date}] [xxx][yyy] (1) [perdir /] go-ahead with proxy request proxy:http://localhost:8585/mytesting/goodOne [OK]

[5]

::1 - - [{date}] [xxx][yyy] (2) [perdir /] rewrite 'var/www/html/testing' -> 'http://localhost:8585/var/www/html/testing'
::1 - - [{date}] [xxx][yyy] (2) [perdir /] escaped URI in per-dir context for proxy, http://localhost:8585/var/www/html/testing -> http://localhost:8585/var/www/html/testing
::1 - - [{date}] [xxx][yyy] (2) [perdir /] forcing proxy-throughput with http://localhost:8585/var/www/html/testing
::1 - - [{date}] [xxx][yyy] (1) [perdir /] go-ahead with proxy request proxy:http://localhost:8585/var/www/html/testing [OK]

[6]

::1 - - [{date}] [xxx][yyy] (2) [perdir /] rewrite 'var/www/html/' -> 'http://localhost:8585/var/www/html/'
::1 - - [{date}] [xxx][yyy] (2) [perdir /] escaped URI in per-dir context for proxy, http://localhost:8585/var/www/html/ -> http://localhost:8585/var/www/html/
::1 - - [{date}] [xxx][yyy] (2) [perdir /] forcing proxy-throughput with http://localhost:8585/var/www/html/
::1 - - [{date}] [xxx][yyy] (1) [perdir /] go-ahead with proxy request proxy:http://localhost:8585/var/www/html/ [OK]

1 个答案:

答案 0 :(得分:0)

您的正则表达式在“测试”之后需要“ / ”,如果这是可选的,如您的第二个示例所示,请更新您的正则表达式以反映这一点,方法是添加,使用%{REQUEST_URI}而不是不必要的(。*)+ $ 1 组合也是一种好习惯,例如。

input()