views:

117

answers:

1

I want to use mod-rewrite to check for a file in two separate locations: at the requested URL, and at the requested URL within the "public" directory.

Here's what I have so far:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$0 !-f  #this line isn't working
RewriteRule ^(.*)$ index.php?$1 [L]

It can correctly determine if the file is there, but it's not correctly determining if the file is at /public/supplied/file/path/file.extension. How do I test for that?

A: 

I've actually realized the problem is that public/ is not in the root directory. It is in the same directory that .htaccess is in. Is there a way to get the directory that .htaccess is in, so this can work in different locations without requiring manual modification of the .htaccess for each?

You can do it a subrequest. The -F flag will treat relative paths as relative to the directory the .htaccess is in. So:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond public/$0 !-F
RewriteRule ^(.*)$ index.php?$1 [L,B]

Note that the -F implies a bigger performance penalty than -f, since instead of just a stat, a subrequest is generated. You should also add the B flag to escape your back reference.

Artefacto