相同的 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$”。由于模式中的正斜杠而无法匹配它。尝试
代替
在您的 .htaccess 文件中。
在您的 .htaccess 中将其添加到 RewriteRule 上方:
RewriteCond %{HTTP_HOST} ^(www.)?/test/test.html$
更改路径以适合您的环境。