DirectoryIndex not working properly for rewritten URL

5,824

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.

Share:
5,824

Related videos on Youtube

mxxk
Author by

mxxk

Updated on September 18, 2022

Comments

  • mxxk
    mxxk almost 2 years

    I have domain.com and sub.domain.com pointing to the same server and I'm using mod_rewrite to rewrite URLs for sub.domain.com to the sub 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 have index.php and no index.html, but requests to http://sub.domain.com seem to ignore index.php altogether and return 404. If I have index.html there, however, its is served. The only way I could get index.php to be served is to set

    DirectoryIndex 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 for sub/search/index.html then sub/search/index.php before returning 404.

    If I query the sub directory from the parent domain http://domain.com/sub, I'm able to see index.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
    mxxk over 9 years
    I actually ended up using a nearly identical workaround. Thanks for posting it!
  • Alastair Irvine
    Alastair Irvine over 5 years
    mod_rewrite statements were in use, which was preventing DirectoryIndex from being checked. This fixed it.