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

On my server (Apache2, Ubuntu 10.04), wpsa is a directory in my webroot. When accessing the url mysite.com/wpsa, a ReWriteRule which should not occur when the requested file is a directory, runs anyway.

These are my rules:

RewriteCond %{REQUEST_URI} !^/index.php$ 
RewriteCond %{REQUEST_URI} !^/$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /000_ROUTE.php [L]

This is from the rewrite log:

applying pattern '.*' to uri '/wpsa/'
RewriteCond: input='/wpsa/' pattern='!^/index.php$' => matched
RewriteCond: input='/wpsa/' pattern='!^/$' => matched
RewriteCond: input='/wpsa/' pattern='!-f' => matched
RewriteCond: input='/wpsa/' pattern='!-d' => matched
rewrite '/wpsa/' -> '/000_ROUTE.php'
local path result: /000_ROUTE.php

It says, "Is 'wpsa' a driectory?" -> "No it's not". But really it is. Why does it think my directory isn't a directory?

If I add:

ReWriteRule ^/wpsa - [L,NC]

before this rule, it loads the directory. But I shouldn't have to create a rule that does nothing for every directory, should I? That's what the -f/-d conditions are for.

share|improve this question

1 Answer 1

Your webroot isn't / (right?), so it's correct that -d didn't get a hit. It's trying to match /wpsa/ to the filesystem, literally - not relative to the webroot.

The reason it's giving you /wpsa/ as your string input instead of /path/to/webroot/wpsa, which would work as expected, is that you're using %{REQUEST_FILENAME} in the wrong context.

When %{REQUEST_FILENAME} is used in a context where the filesystem mapping is already done, like in a <Directory> block or .htaccess file, the full path is returned and -d will work as you're expecting.

From the docs:

REQUEST_FILENAME

The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.

You seem to be using it in a <VirtualHost> or <Location> context, which makes it functionally identical to %{REQUEST_URI}. If you'd like to keep it in that context, try:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
share|improve this answer
    
+1, good explanation. I'm going to try this out when I get back later and accept. I guess I assumed that starting with a forward slash meant the web-root in this context too since it does in rewrite rules, etc. And you're correct, this is in the vhost not .htaccess. –  Nick Dec 6 '12 at 12:33

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.