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:


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


  • /directorywith_no_defaultfile/

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

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?

  • Can you add the relevant bits of your .htaccess file to the question? – meulop May 13 '10 at 21:30
  • I was hoping I would not get asked that :o) - The real .htaccess file is constantly changing as I hack away and experiment But, yes. I will make a start on trying to piece together an example that works as a kind of 'proof of concept'. Was mainly wondering if anyone had experienced this before could say "aaah! I had this issue the basic principal is to ensure xyz to avoid it" – j w May 13 '10 at 22:04

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.

  • yep. that solves it thanks. (It limits it to one 'mod-rewritten default bootstrap for directories with no default file' per .htaccess - but i can live with that.) – j w May 14 '10 at 19:12

This is how I generally setup my mod_rewrite script in the .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    rewriterule ^(.*)$ http://www.domain-name.com/$1 [r=301,nc]

Hope that helps!

  • yes, always handy to see how others do it. – j w May 14 '10 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.