Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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]    
</IfModule>

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
add comment

2 Answers

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! –  user706472 May 5 '12 at 14:41
add comment

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 “http://domain.com/foo”. 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
add comment

Your Answer

 
discard

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.