DirectoryIndex对于重写的URL不能正常工作

我有domain.comsub.domain.com指向相同的服务器,我使用mod_rewrite重写sub.domain.comsub子目录的URL。 我在文档根目录中有以下.htaccess文件:

 DirectoryIndex index.html index.php # Prevent infinite rewrite loop. RewriteCond %{ENV:REDIRECT_STATUS} ^$ # Send all requests addressing sub.domain.com... RewriteCond %{HTTP_HOST} =sub.domain.com [NC] # ...to the /sub directory. RewriteRule ^ /sub%{REQUEST_URI} [QSA] 

sub我有index.php和没有index.html ,但请求http://sub.domain.com似乎完全忽略index.php并返回404。如果我有index.html那里,但是,它是提供服务。 我能得到index.php服务的唯一方法就是设置

 DirectoryIndex index.php 

但是这不是我想为整个网站做的事情。

奇怪的是,文档根以外的URL显示正常的DirectoryIndex行为。 例如, http://sub.domain.com/search //sub.domain.com/search试图在返回404之前查找sub/search/index.html然后是sub/search/index.php

如果我从父域http://domain.com/sub查询sub ,我能够看到index.php ,这让我完全傻眼的问题。

我会包括Apache错误日志,但我使用共享主机,并没有办法增加日志冗长。 另外,我无法在本地重现此错误。 networking托pipe服务器使用Apache 2.4.3,我的本地服务器是Apache 2.4.9。

升级到包含Apache 2.4.7的Ubuntu 14.04后,我遇到类似的问题。

这是我的解决方法 。 使用mod_rewrite 模拟 mod_dir ,并禁用DirectoryIndex

 #DirectoryIndex index.html index.php index.htm DirectoryIndex disabled RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_FILENAME} /$ RewriteCond %{REQUEST_FILENAME}index.html -f RewriteRule ^(.*)$ $1index.html [L] RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_FILENAME} /$ RewriteCond %{REQUEST_FILENAME}index.php -f RewriteRule ^(.*)$ $1index.php [L] RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_FILENAME} /$ RewriteCond %{REQUEST_FILENAME}index.htm -f RewriteRule ^(.*)$ $1index.htm [L] 

希望这可以帮助。


更新 :升级到Apache 2.4.10解决了我的问题。

感谢OndřejSurý,这可以在Ubuntu 14.04上用apt:

 add-apt-repository ppa:ondrej/apache2 apt-get update apt-get install apache2 

PS OP确认这也是在Apache 2.4.9中修复的。