3

Apache 2.4 中的ENDmod_rewrite 标志似乎无法正常工作。我有一个以test文件命名的目录test.html。我.htaccess在目录中放置了一个文件,内容如下

RewriteEngine on

RewriteRule ^test$ test.html [NC,QSA,END]
RewriteRule ^$ test.html  [NC,QSA,END]

这是请求http://localhost.dev/test/的重写日志

[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/ -> 
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri ''
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/ -> 
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri ''
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] rewrite '' -> 'test.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] add per-dir prefix: test.html -> /var/www/vhosts/test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip document_root prefix: /var/www/vhosts/test/test.html -> /test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] internal redirect with /test/test.html [INTERNAL REDIRECT]
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.php -> index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'index.php'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.php -> index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri 'index.php'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] pass through /var/www/vhosts/test/index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.html -> index.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'index.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.html -> index.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri 'index.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] pass through /var/www/vhosts/test/index.html

当第二条规则与请求匹配时,为什么 mod_rewrite 对目录索引文件index.html和发出子请求?index.php

请求http://localhost.dev/test/test没有任何子请求,正如预期的那样

[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/test -> test
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'test'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] rewrite 'test' -> 'test.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] add per-dir prefix: test.html -> /var/www/vhosts/test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] strip document_root prefix: /var/www/vhosts/test/test.html -> /test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] internal redirect with /test/test.html [INTERNAL REDIRECT]

我尝试过独立和一起使用L和标志。END

4

2 回答 2

2

看来 mod_dir 不尊重 mod_rewrite 设置的 END 标志。对于遇到这种情况的其他人,我可以通过以下方式解决该问题

RewriteEngine on
RewriteRule ^$ /test/home.php  [NC,QSA,END,E=IS_DIR_INDEX:1]
FallbackResource /test/fallback.php

<If "env('IS_DIR_INDEX') == 1">
    SetHandler default_handler
</If> 
于 2013-07-28T08:42:03.140 回答
1

我在 Ubuntu 上的 Apache 2.4.7 上遇到了同样的问题,并向 Apache 报告了一个错误,该错误已在 2.4.9 中得到确认并修复,请参见此处对于 Ubuntu,这里有一个更新的 PPA以便于升级。如果无法升级或者您不想编译源代码,您可以使用上面 Joyce 的解决方法,或者我在此处发布的仅使用 RewriteCond 和 RewriteRule 的解决方法。

于 2014-04-20T17:59:28.703 回答