mod_rewrite:仅在没有以前的规则匹配时才匹配吗? [英] mod_rewrite: match only if no previous rules have matched?

查看:32
本文介绍了mod_rewrite:仅在没有以前的规则匹配时才匹配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的重写规则,如下所示:

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

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

然后我有一种包罗万象的规则,我想仅在上述规则不匹配时才适用(例如,对于/blatz).只要我记得包括[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)

推荐答案

我能想到的唯一解决方案是使用S标志跳过最后一条规则:

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 …

或设置环境变量:

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


编辑.好的,这是另一种解决方案,它将当前URL与最初请求的URL进行比较:


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 …

但是我认为至少需要Apache 2,因为Apache 1.x使用POSIX ERE和POSIX ERE在模式中不支持\ n 后向引用.

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.

这篇关于mod_rewrite:仅在没有以前的规则匹配时才匹配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆