如何使用“与",“或"在Apache上使用RewriteCond? [英] how to use "AND", "OR" for RewriteCond on Apache?

查看:51
本文介绍了如何使用“与",“或"在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?

推荐答案

这是一个有趣的问题,由于在文档中没有非常明确地说明,因此我将通过

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 ).

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

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