DirectoryIndex not working properly for rewritten URL
Solution 1
I have a similar issue after upgrading to Ubuntu 14.04, which includes Apache 2.4.7.
Here is my workaround. Emulate mod_dir
with mod_rewrite
, and disable 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]
Hope this helps.
UPDATE: Upgrading to Apache 2.4.10 fix the issue for me.
Thanks to Ondřej Surý this can be done easily on Ubuntu 14.04 with apt:
add-apt-repository ppa:ondrej/apache2
apt-get update
apt-get install apache2
p.s. OP confirmed this is fixed in Apache 2.4.9 too.
Solution 2
Set the DirectoryCheckHandler directive to ON
.
The Apache docs say:
Available in 2.4.8 and later. Releases prior to 2.4 implicitly act as if "DirectoryCheckHandler ON" was specified.
mod_dir used to always perform its checks no matter which handler was set and now it must be explicitly enabled when not using the default handler.
Related videos on Youtube
![mxxk](https://i.stack.imgur.com/iZ4RR.png?s=256&g=1)
mxxk
Updated on September 18, 2022Comments
-
mxxk almost 2 years
I have
domain.com
andsub.domain.com
pointing to the same server and I'm using mod_rewrite to rewrite URLs forsub.domain.com
to thesub
subdirectory. I have the following.htaccess
file in the document root: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]
Within the
sub
directory I haveindex.php
and noindex.html
, but requests tohttp://sub.domain.com
seem to ignoreindex.php
altogether and return 404. If I haveindex.html
there, however, its is served. The only way I could getindex.php
to be served is to setDirectoryIndex index.php
but that is not something I want to do for the entire site.
Oddly enough, URLs other than the document root exhibit normal
DirectoryIndex
behavior. For example,http://sub.domain.com/search
tries looks forsub/search/index.html
thensub/search/index.php
before returning 404.If I query the
sub
directory from the parent domainhttp://domain.com/sub
, I'm able to seeindex.php
, which leaves me completely dumbfounded with the issue.I'd include the Apache error log, but I'm using shared hosting and have no way to increase logging verbosity. Also, I was unable to reproduce this error locally. The web hosting server is using Apache 2.4.3, and my local server is Apache 2.4.9.
-
mxxk over 9 yearsI actually ended up using a nearly identical workaround. Thanks for posting it!
-
Alastair Irvine over 5 years
mod_rewrite
statements were in use, which was preventingDirectoryIndex
from being checked. This fixed it.