Mod rewrite works fine apart from for missing directory index files

.htaccessapache-2.2mod-rewrite

I have a legacy web site hosted on Apache.
It has a number of web pages sitting in the public web root and its subfolders.

For example's sake, lets say these are the only files and folders in my web root:

publicDocs/
directorywith_no_defaultfile/
    some-legacy-flat-page.htm
.htaccess
index.php
some-legacy-flat-page.htm

I would like to start using Zend MVC for some of the newer pages.

I have got a .htaccess mod rewrite rule working so that any request for a non-existent file is sent to be handled by the MVC bootstrap file (/index.php).

With my current set-up, the following types of requests are successfully routed to '/index.php', the MVC bootstrap:

  • /index.php
  • /blah
  • /directorywith_no_defaultfile/bloo

The following types of request are successfully served by old legacy (flat) pages

  • /some-legacy-flat-page.htm
  • /directorywith_no_defaultfile/some-legacy-flat-page.htm

But, when I request a non-existent file that is a directory like these:

  • /directorywith_no_defaultfile

or

  • /directorywith_no_defaultfile/

instead of routing these to the MVC bootsrap, I get an error:

Forbidden    
You don't have permission to access /directorywith_no_defaultfile/ on this server.    
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I suspect this may have something to do with the way Apache handles default files.

Currently, the only way to solve it is to:

  • delete that directory (which is not possible while it holds flat legacy pages), or
  • put an index.htm file in there which contains a script that include()'s the bootstrap file

Do you know which Apache directives could be causing this 'Forbidden' error?

Are the any directives i can use in .htaccess to tell Apache to 'chill out' about missing default files in directories and just leave it to the mod-rewrite to worry about?

Best Answer

The documentation for the DirectoryIndex directive includes the following:

Note that the documents do not need to be relative to the directory;

DirectoryIndex index.html index.txt /cgi-bin/index.pl

would cause the CGI script /cgi-bin/index.pl to be executed if neither
index.html or index.txt existed in a directory.

So, adding the following line in the appropriate places should do it:

DirectoryIndex index.html index.php /index.php

Tweak this to include support for whatever types of index files you want to be handled normally. As long as "/index.php" is the last one, it should do what you want.