mod_rewrite: match only if no previous rules have matched?

筅森魡賤 提交于 2019-12-12 01:17:01

问题


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:


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.



来源:https://stackoverflow.com/questions/1500981/mod-rewrite-match-only-if-no-previous-rules-have-matched

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!