2

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?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Should be posted on ServerFault – Steen Feb 08 '10 at 12:31
  • Yeah, I had that discussion already with my last mod_rewrite question. Well, if five people feel that way then migrate it if you must, but as long as we're answering *all sorts* of HTML questions here on SO that have *much, much less* to do with programming than any mod_rewrite question, I will continue posting mod_rewrite questions here. (not attacking you in any way here @Steen. This just needed to get out at the right occasion, which happened to be this question.) – Pekka Feb 08 '10 at 12:33
  • Cheers. Glad to give the opportunity :) What's important is that you get your question answered, which it seems you did. – Steen Feb 08 '10 at 22:07

1 Answers1

1

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
  • 643,351
  • 109
  • 780
  • 844
  • (slaps head) of course. Thanks Gumbo. Can I rely on `[L]` working in this context despite what the quote says? – Pekka Feb 08 '10 at 12:35
  • 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 Feb 08 '10 at 12:38
  • Strange. Does anybody know why `-d` does *not* apply to resources requested with a trailing slash `www.example.com/directory/`? – Pekka Feb 08 '10 at 12:47
  • @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 Feb 08 '10 at 12:55
  • 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 Feb 08 '10 at 13:02