相同的Apache重写规则在VHost中工作(匹配),但不在“.htaccess”文件中:
<VirtualHost *:80> ServerName 192.168.1.100 ServerAlias example.com DocumentRoot /test RewriteEngine On RewriteRule ^/test.html$ /test2.html RewriteLog "/tmp/rewrite.log" RewriteLogLevel 9 <Directory "/"> AllowOverride All </Directory> </VirtualHost>
重写日志:
192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) init rewrite engine with requested uri /test.html 192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (3) applying pattern '^/test.html$' to uri '/test.html' 192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) rewrite '/test.html' -> '/test2.html' 192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) local path result: /test2.html 192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) prefixed with document_root to /test/test2.html 192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (1) go-ahead with /test/test2.html [OK]
“.htaccess文件”
# cat /test/.htaccess RewriteEngine On RewriteRule ^/test.html$ /test2.html
重写日志:
192.168.1.10 - - [13/May/2013:16:22:17 +0100] [example.com/sid#7fd76455b7d0][rid#7fd764af0d78/initial] (3) [perdir /test/] strip per-dir prefix: /test/test.html -> test.html 192.168.1.10 - - [13/May/2013:16:22:17 +0100] [example.com/sid#7fd76455b7d0][rid#7fd764af0d78/initial] (3) [perdir /test/] applying pattern '^/test.html$' to uri 'test.html' 192.168.1.10 - - [13/May/2013:16:22:17 +0100] [example.com/sid#7fd76455b7d0][rid#7fd764af0d78/initial] (1) [perdir /test/] pass through /test/test.html
根据第二个日志,它正在寻找“test.html”URL中的“^ / test.html $”。 由于模式中的正斜杠而无法匹配。 尝试
RewriteRule ^test.html$ /test2.html
代替
RewriteRule ^/test.html$ /test2.html
在你的.htaccess文件中。
在你的.htaccess的RewriteRule上面添加:
RewriteCond%{HTTP_HOST} ^(www。)?/ test / test.html $
更改path以适合您的环境。