如何使用“AND"、“OR"用于 Apache 上的 RewriteCond? [英] how to use "AND", "OR" for RewriteCond on Apache?

查看:19
本文介绍了如何使用“AND"、“OR"用于 Apache 上的 RewriteCond?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何在 Apache 上对 RewriteCond 使用 AND、OR 吗?

Is this how to use AND, OR for RewriteCond on Apache?

rewritecond A [or]
rewritecond B
rewritecond C [or]
rewritecond D
RewriteRule ... something

变成 if ( (A or B) and (C or D) ) rewrite_it.

所以似乎OR"的优先级高于AND"?有没有一种方法可以轻松分辨,例如 (A or B) and (C or D) 语法?

So it seems like "OR" is higher precedence than "AND"? Is there a way to easily tell, like in the (A or B) and (C or D) syntax?

推荐答案

这是一个有趣的问题,因为在文档中没有非常明确地解释它,我将通过 mod_rewrite的源代码;展示开源的巨大优势.

This is an interesting question and since it isn't explained very explicitly in the documentation I'll answer this by going through the sourcecode of mod_rewrite; demonstrating a big benefit of open-source.

在顶部,您会很快发现 定义用于命名这些标志:

In the top section you'll quickly spot the defines used to name these flags:

#define CONDFLAG_NONE               1<<0
#define CONDFLAG_NOCASE             1<<1
#define CONDFLAG_NOTMATCH           1<<2
#define CONDFLAG_ORNEXT             1<<3
#define CONDFLAG_NOVARY             1<<4

并搜索 CONDFLAG_ORNEXT 确认它是基于 关于 [OR] 标志的存在:

and searching for CONDFLAG_ORNEXT confirms that it is used based on the existence of the [OR] flag:

else if (   strcasecmp(key, "ornext") == 0
         || strcasecmp(key, "OR") == 0    ) {
    cfg->flags |= CONDFLAG_ORNEXT;
}

接下来出现的标志是实际实现 在这里你会找到一个循环,它遍历了 RewriteRule 的所有 RewriteConditions,它的基本作用是(为了清晰起见,添加了注释):

The next occurrence of the flag is the actual implementation where you'll find the loop that goes through all the RewriteConditions a RewriteRule has, and what it basically does is (stripped, comments added for clarity):

# loop through all Conditions that precede this Rule
for (i = 0; i < rewriteconds->nelts; ++i) {
    rewritecond_entry *c = &conds[i];

    # execute the current Condition, see if it matches
    rc = apply_rewrite_cond(c, ctx);

    # does this Condition have an 'OR' flag?
    if (c->flags & CONDFLAG_ORNEXT) {
        if (!rc) {
            /* One condition is false, but another can be still true. */
            continue;
        }
        else {
            /* skip the rest of the chained OR conditions */
            while (   i < rewriteconds->nelts
                   && c->flags & CONDFLAG_ORNEXT) {
                c = &conds[++i];
            }
        }
    }
    else if (!rc) {
        return 0;
    }
}

你应该能够理解这一点;这意味着 OR 具有更高的优先级,并且您的示例确实导致 if ( (A OR B) AND (C OR D) ).例如,如果您有以下条件:

You should be able to interpret this; it means that OR has a higher precedence, and your example indeed leads to if ( (A OR B) AND (C OR D) ). If you would, for example, have these Conditions:

RewriteCond A [or]
RewriteCond B [or]
RewriteCond C
RewriteCond D

它会被解释为 if ( (A OR B OR C) and D ).

这篇关于如何使用“AND"、“OR"用于 Apache 上的 RewriteCond?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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