tags:

views:

46

answers:

1

I've got a large set of rewrite rules like the following:

RewriteRule ^foo foo.php?blah [L]
RewriteRule ^bar foo.php?baz [L]

And then I have a sort of catch-all rule that I want to only apply if the above rules don't match (e.g. for, say /blatz). As long as I remember to include the [L], that works fine -- but I've already had issues twice with accidentally forgetting it.

Is there any easy way to to force my catch-all rule to not match if an earlier rule has matched? (ideally, without appending something to every rule)

+1  A: 

The only solution that I can image is to either use the S flag to skip the last rule:

RewriteRule ^foo foo.php?blah [L,S=999]
RewriteRule ^bar foo.php?baz [L,S=999]
RewriteRule …

Or to set an environment variable:

RewriteRule ^foo foo.php?blah [L,E=FLAG:1]
RewriteRule ^bar foo.php?baz [L,E=FLAG:1]
RewriteCond %{ENV:FLAG} ^$
RewriteRule …


Edit    Alright, here’s another solution that compares the current URL with the originally requested one:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^?\s]*)\??([^\s]*)
RewriteCond %{REQUEST_URI}?%{QUERY_STRING}<%1?%2 ^([^<]*)<\1$
RewriteRule …

But I think that requires at least Apache 2 because Apache 1.x used POSIX ERE and POSIX ERE don’t support the \n backreferences in the pattern.

Gumbo
Ah hah! The "make sure the URL hasn't already changed" solution is clever.
Frank Farmer