Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory named 'foo' within my virtual host root. I am trying to intercept requests for a non-existent file also called 'foo' and rewrite them to a PHP file, the .htaccess file looks like:

<IfModule mod_rewrite.c>
    RewriteEngine on   
    RewriteBase /
    RewriteCond %{REQUEST_URI} /foo
    RewriteRule ^(.*)$ router.php [L]    

All I keep getting back from the server on a GET request for '/foo' is a 301 redirect to '/foo/' (the directory). Why is Apache doing this before processing the rules? Am I missing a configuration option? Or can this not be done?

share|improve this question
up vote 1 down vote accepted

The 301 redirect is coming from the DirectorySlash directive that is part of mod_dir.

I suspect your .htaccess is not working due to one or both of:

  1. You have AllowOverride configured to not allow .htaccess files.
  2. The .htacess file is inside the /foo/ directory, so mod_dir applies its 301 redirect before the .htaccess file is processed.
share|improve this answer
The DirectorySlash directive was the configuration that I was missing. Thanks! – olsgreen May 5 '12 at 14:41

Remember the Filesystem Always Takes Precedence

The filesystem on your server will always take precedence over the rewritten URL. For example, if you have a directory named “foo” and within that directory is a file called “router.php”, you can’t have the URL redirect to “”. What happens is that Apache goes into the “foo” directory and doesn’t see the rewrite instructions.

To fix this, simply rename your directory (adding an underscore to the beginning or end is a simple way to do that).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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