Apache 重写规则与“.htaccess”文件不匹配

Apache 重写规则与“.htaccess”文件不匹配

相同的 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

答案1

根据第二条日志,它在“test.html”URL 中查找“^/test.html$”。由于模式中有正斜杠,因此无法匹配。尝试

RewriteRule ^test.html$ /test2.html

代替

RewriteRule ^/test.html$ /test2.html

在您的.htaccess 文件中。

答案2

在 .htaccess 中的 RewriteRule 上方添加此内容:

RewriteCond %{HTTP_HOST} ^(www.)?/test/test.html$

更改路径以适合您的环境。

相关内容