views:

26

answers:

1

I have a .htaccess file with a number of RewriteRules.

How can I exclude already existing files and directories from having run through each of the RewriteCond / RewriteRule pairs?

This:

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .*  - [F,L]  

won't work reliably because according to this SO question:

within the .htaccess context, [L] will not force mod_rewrite to stop. it will continue to trigger internal sub-requests:

How can I make this work? Alternatively, is ther a control structure like if condition applies, go through this block of rules, otherwise skip and go to end in mod_rewrite?

+1  A: 

The problem with your rule is that both conditions can not be fulfilled at the same time because a regular file (-f) cannot also be a directory (-d). Try this instead:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
Gumbo
(slaps head) of course. Thanks Gumbo. Can I rely on `[L]` working in this context despite what the quote says?
Pekka
Should be ok, you can always add the [NS] flag to the rule (ie. [L,NS]) which means ignore for internal sub-requests. Best way is to suck it and see unfortunately, mod_rewrite is a bit of a black art :)
Paolo
Strange. Does anybody know why `-d` does *not* apply to resources requested with a trailing slash `www.example.com/directory/`?
Pekka
@Pekka: The *L* flag does only cause an internal redirect if the URI is changed. So your rule won’t cause an internal redirect.
Gumbo
@Gumbo Ah I see, thanks!
Pekka
I have a related issue in case anybody's interested: http://stackoverflow.com/questions/2218971/how-can-i-make-the-f-flag-apply-to-directories-with-a-trailing-slash
Pekka