使用RewriteRule时提供本地内容的麻烦[P]

时间:2013-09-13 19:11:58

标签: apache mod-rewrite reverse-proxy

我正在尝试编写将在新服务器上提供文件的规则,同时将不存在的文件代理到旧服务器。代理规则工作正常,将丢失的文件和目录发送到旧服务器。

我遇到的问题是new-server.example.com/blah(/ blah /在新服务器上存在)被传递到代理规则并路由到旧服务器。

尝试跳过的原因是因为new-server.example.com/blah会进入“代理部分”并传递new-server.example.com/blah/index.php。 index.php在新服务器上不存在,index.html确实存在。 “代理部分”不遵循DirectoryIndex中的所有索引文件可能性,它只是查看第一个。

所以我最终收到错误消息,“在此服务器上找不到请求的URL /blah/index.php。”

如何写这样才真正起作用? - 谢谢。

RewriteEngine On
# skip, proxy section
RewriteCond /var/www/html%{REQUEST_FILENAME}     -f  [OR]
RewriteCond /var/www/html%{REQUEST_FILENAME}     -d
RewriteRule .? - [S=4]

# proxy section
RewriteCond /var/www/html%{REQUEST_FILENAME}       !-f
RewriteCond /var/www/html%{REQUEST_FILENAME}       !-d
RewriteRule ^/(.*) http://old-server.example.com/$1 [P]
ProxyPassReverse / http://old-server.example.com/


# rewrite log
pass through /index.php
init rewrite engine with requested uri /
applying pattern '.?' to uri '/'
RewriteCond: input='/var/www/html/' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/' pattern='-d' => matched
pass through /
init rewrite engine with requested uri /index.php
applying pattern '.?' to uri '/index.php'
RewriteCond: input='/var/www/html/index.php' pattern='-f' => matched
pass through /index.php
init rewrite engine with requested uri /blah/
applying pattern '.?' to uri '/blah/'
RewriteCond: input='/var/www/html/blah/' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/blah/' pattern='-d' => matched
pass through /blah/
init rewrite engine with requested uri /blah/index.php
applying pattern '.?' to uri '/blah/index.php'
RewriteCond: input='/var/www/html/blah/index.php' pattern='-f' =>    not-matched
RewriteCond: input='/var/www/html/blah/index.php' pattern='-d' => not-matched
applying pattern '^/(.*)' to uri '/blah/index.php'
RewriteCond: input='/var/www/html/blah/index.php' pattern='!-f' => matched
RewriteCond: input='/var/www/html/blah/index.php' pattern='!-d' => matched
rewrite '/blah/index.php' -> 'http://old-server.example.com/blah/index.php'
forcing proxy-throughput with http://old-server.example.com/blah/index.php
go-ahead with proxy request proxy:http://old-server.example.com/blah/index.php [OK]

1 个答案:

答案 0 :(得分:0)

这是Apache自2.0版以来的工作方式:它获取请求,通过所有RewriteRules运行该请求,然后如果生成的请求是目录,它会尝试查看第一个DirectoryIndex是否可以工作,所以它通过RewriteRules运行并尝试提供结果。如果第一个DirectoryIndex条目不起作用(即它到达Apache将服务404的点),则服务器再次尝试下一个条目,依此类推。显然,您的问题是代理规则会锁定不存在的第一个DirectoryIndex值,并尝试通过反向代理发送它。

我还没有找到在服务器范围内修复此行为的一般方法,但有一种解决方法。使用<Location>文件中的httpd.conf部分或.htaccess文件,并覆盖该地点的DirectoryIndex

在您的情况下,创建文件/var/www/html/blah/.htaccess并将以下内容放入其中:

DirectoryIndex index.html