Skip to content
AnswerWell geo Documentation

Research · 10 min read · Answerwell

Rule Expression Language: Operators, Compound Logic, Fields, and Limits

How HTTP rule expressions work: comparison and logical operators, fields, string and regex syntax, product limits, and validation.

This page summarises Cloudflare documentation on the Rules language—the DSL used to match HTTP requests (and related traffic) in products such as WAF custom rules, Transform Rules, Cache Rules, Rate limiting rules, and Load Balancing rules. The language is described as flexible, intuitive, and based on Wireshark display filters. Gateway policies for Cloudflare One use a related but separate expression syntax; both are covered where the cluster documents overlap.

Rule expressions: what simple and compound forms mean

Section titled “Rule expressions: what simple and compound forms mean”

A rule runs only when its expression (filter) evaluates to true for the current request or packet.

The Rules language supports two kinds of expressions: simple and compound.

— Cloudflare, Expressions

Simple expressions compare one field from the request (or response, where supported) to a value you supply, using a comparison operator:

Simple expressions have the following syntax:

<field> <comparison_operator> <value>

— Cloudflare, Expressions

When the comparison is true, the request matches. Example from Load Balancing docs: (http.request.uri.path contains "/content").

Compound expressions join two or more sub-expressions with logical operators such as and or or:

Compound expressions use logical operators such as and to combine two or more expressions into a single expression.

— Cloudflare, Expressions

General pattern:

<expression> <logical_operator> <expression>

A compound expression can itself be an operand of another logical operator, so you can build multi-clause policies—for example targeting www.example.com while excluding certain edge ports:

http.host eq "www.example.com" and not cf.edge.server_port in {80 443}

(as in Expressions).

Load Balancing documentation uses the same split:

Simple expressions compare a value from an HTTP request to a value defined in the expression. A simple expression is identified by the presence of a comparison operator (equals or less than, for example).

Compound expressions combine two or more simple expressions into a single expression. Compound expression contains a logical operator (and, or, for example).

— Cloudflare, Load Balancing expressions

When a request hits the network, Cloudflare builds field–value pairs to match against:

When an HTTP request reaches the Cloudflare global network, Cloudflare creates a table of field–value pairs against which to match expressions.

— Cloudflare, Values

Fields can be primitive (from the request), derived (functions such as lower()), or computed (for example cf.waf.score* attack scores). See Fields for request, response, raw, and dynamic categories.

Comparison operators: how a field must relate to your value

Section titled “Comparison operators: how a field must relate to your value”

Comparison operators answer whether the live value equals, contains, matches regex, is in a set, and so on.

Comparison operators specify how values defined in an expression must relate to the actual HTTP request value for the expression to return true.

— Cloudflare, Operators and grouping symbols

Pattern:

<field> <comparison_operator> <value>

The Rules language supports both English names (eq, ne, lt, contains, matches, in, …) and C-like symbols (==, !=, ~ for regex in some contexts). English operators must be lowercase:

Comparison operators entered using English notation (such as eq, lt, and gt) must be written in lowercase.

— Cloudflare, Operators and grouping symbols

Name Notation (examples) Typical use
Equal / not equal eq / ==, ne / != Paths, hosts, IPs, numbers
Order lt, le, gt, ge Scores, numeric fields (not all types support every operator)
Substring contains URI paths, strings
Wildcards wildcard, strict wildcard Case-insensitive vs case-sensitive path patterns
Regex matches / ~ Business and Enterprise plans for matches
Sets and lists in IPs, ASNs, inline sets; in $<LIST> for account lists where supported

Full operator tables and data-type support are in Operators and grouping symbols. Cache Rules in the dashboard expose a overlapping set (equals, matches regex, is in list, etc.); not every operator applies to every field—see Cache Rules settings.

String comparison in rule expressions is case-sensitive.

— Cloudflare, Edit expressions in the dashboard

Mitigations documented in Values: case-insensitive wildcard, lower() with a lowercased literal, multiple or branches, or matches with a regex (plan permitting).

IP ranges: CIDR in sets, not bare equality

Section titled “IP ranges: CIDR in sets, not bare equality”

Wireshark-style ip.src == 1.2.3.0/24 is not supported in the Rules language; use in with a set:

To evaluate a range of addresses using CIDR notation, use the in comparison operator as in this example: ip.src in {1.2.3.0/24 4.5.6.0/24}.

— Cloudflare, Fields

Network firewall expressions repeat the same rule for packet filters:

ip.src == 192.0.2.0/24 # bad
ip.src in { 192.0.2.0/24 } # good

— Cloudflare, Form expressions (packet filtering)

Regex matching uses the Rust regex engine. Cloudflare recommends testing with external tools mentioned in WAF FAQ and Operators.

Hard limits on the Rules language side:

The maximum length of a rule expression is 4,096 characters.

— Cloudflare, Expressions

Each rule can contain a maximum of 64 regular expressions in its expression.

— Cloudflare, Expressions

Rules above the regex count cannot be created or updated; existing overweight rules keep working until you simplify them. Strategies to reduce regex count are listed under Values — Regular expression limits.

Network DDoS override expressions have a separate 4,000-character cap per expression (network override expressions).

Logical operators: combining clauses and order of evaluation

Section titled “Logical operators: combining clauses and order of evaluation”

Logical operators combine two or more expressions into a single compound expression.

— Cloudflare, Operators and grouping symbols

Operator English C-like Precedence (1 = first)
NOT not ! 1
AND and && 2
XOR xor ^^ 3
OR or || 4

Since the logical and operator has precedence over logical or, the and operator must be evaluated first.

— Cloudflare, Operators and grouping symbols

So Expression1 and Expression2 or Expression3 means (Expression1 and Expression2) or Expression3, not Expression1 and (Expression2 or Expression3), unless you add parentheses.

English logical operators must be lowercase, same as comparison operators.

Parentheses are grouping symbols for precedence and nesting; only the Expression Editor and API support them—the visual Expression Builder does not (Operators — Grouping symbols, Edit expressions).

Complexity limits on large compound expressions

Section titled “Complexity limits on large compound expressions”

Very large boolean trees can hit engine limits. Packet and network firewall docs warn that long port-disjunction chains may return HTTP 400 with ruleset exceeds complexity constraints; the fix is to split across multiple rules evaluated in order (Form expressions (Network Firewall), packet filtering Form expressions).

Gateway policy expressions vs Ruleset Engine language

Section titled “Gateway policy expressions vs Ruleset Engine language”

Cloudflare One Gateway policies (DNS, HTTP, network, egress, resolver) use text expressions with operators such as ==, !=, in, and matches, plus and, or, and not:

Gateway expressions follow this pattern:

<field> <operator> <value>

— Cloudflare, Gateway policy expression syntax

Aspect Ruleset Engine (Rules language) Gateway
Typical products WAF, Transform, Cache, Rate limiting DNS / HTTP / Network / Egress policies
Example fields http.request.uri.path, cf.bot_management.score dns.fqdn, identity.email
Identity fields Not available Available (identity.email, identity.groups, …)

Do not reference the Ruleset Engine fields reference when building Gateway policies. Gateway has its own field set documented on each policy type page.

— Cloudflare, Gateway policy expression syntax

Dashboard Gateway builders expose operators like is, matches regex, and in list on selectors (DNS policies, HTTP policies). Gateway regex is also evaluated with Rust; the pipe | acts as OR inside a single regex value without escaping the pipe (HTTP policies — Regular expressions).

Or across policy groups: Gateway docs state that Or only works within the same expression group—you cannot OR a Traffic condition with an Identity condition in one flat expression (DNS policies — Logical operators).

Access policies use a different model (Include, Require, exclude) with documented boolean meaning (infrastructure apps, Access policies)—not the same as WAF rule expressions.

Expression Builder vs Expression Editor: when to use which

Section titled “Expression Builder vs Expression Editor: when to use which”

In the dashboard you can usually switch between:

Expression Builder: Allows you to create expressions using drop-down lists, emphasizing a visual approach to defining an expression.
Expression Editor: A text-only interface that supports advanced features, such as grouping symbols and functions for transforming and validating values.

— Cloudflare, Edit expressions in the dashboard

The Builder does not support nested expressions or function calls, and may not list every field. If you enter unsupported syntax in the Editor, switching back triggers a warning: discard changes or stay in the Editor (Edit expressions).

The Expression Preview shows the text form of what you built—for example (ip.src.country ne "GB") from the Builder docs.

String literals and regex: quoted vs raw syntax

Section titled “String literals and regex: quoted vs raw syntax”

Two string formats exist; both can carry regex patterns:

You can use either of the two string formats to specify regular expressions in an expression. However, Cloudflare recommends that you use the raw string syntax, since the quoted string syntax has complex escaping rules and can lead to unexpected behaviors if not thoroughly tested.

— Cloudflare, Values — String values and regular expressions

Quoted strings use "..." and require escaping \ and " as \\ and \". Regex on the right-hand side of matches follows regex escaping rules on top of string escaping—double-escaping is sometimes required in functions like regex_replace() (Values — Quoted string syntax).

Raw strings use delimiters such as r#"..."# or r##"..."##; characters are literal until the closing delimiter, except regex metacharacters still matter inside regex patterns (Values — Raw string syntax).

The Builder can auto-escape \ and " in quoted literals; choosing Matches regex in the operator dropdown tends to preview raw syntax (Edit expressions — Escape special characters).

WAF FAQ confirms: raw syntax only needs regex-special escaping; quoted syntax needs extra escaping for " and \ in literals and regex.

For http.request.headers, header names must be lowercase:

To obtain the value of an HTTP request header using the http.request.headers field, specify the header name in lowercase.

— Cloudflare, URL rewrite — Available fields and functions

Example: http.request.headers["accept-encoding"][0]. Use to_string() when you need a string from a non-string value (same page).

Bulk Redirects and the in operator with lists

Section titled “Bulk Redirects and the in operator with lists”

Default bulk redirect filter:

http.request.full_uri in $<LIST_NAME>

You may narrow matches—for example by country:

ip.src.country == "GB" and http.request.full_uri in $<LIST_NAME>

At the left of the in operator you can only use fields directly and not values returned by a function.

— Cloudflare, Bulk Redirects concepts

Common mistakes when writing rule expressions

Section titled “Common mistakes when writing rule expressions”
  1. Ambiguous and / or without parentheses — Rely on precedence only when it matches your intent; otherwise group explicitly (Operators — Order of precedence).

  2. CIDR on eq / == — Use in { … } for ranges (Fields, Form expressions).

  3. Wrong field catalog for Gateway — Rules language fields such as http.request.uri.path are not interchangeable with Gateway’s field docs (Gateway expression syntax).

  4. Case-sensitive string compares — /Login and /login differ unless you normalise or use wildcards (Edit expressions).

  5. Dashboard ends with vs language functions — Custom text must use ends_with(http.request.uri.path, ".html"), not ends_with as a faux operator (Operators — Additional operators in the dashboard).

  6. Nil vs empty string in comparisons — Comparing missing URI args can yield surprising eq results; coalesce() may be needed (Values — Comparisons).

  7. Too many regexes or regex complexity — Hitting the 64-regex-per-rule cap or Chrome extension limits (below) requires simplifying patterns.

Cloudflare validates expressions before save; parse errors surface in the dashboard, e.g. Filter parsing error … unrecognised input (Edit expressions — Expression validation).

  1. In the Cloudflare dashboard, open the product where your rule lives (for example WAF custom rules or Cache Rules) and use Edit expression on the rule to open the Expression Editor. Confirm the Expression Preview matches the syntax you intend before saving.

  2. For regular expressions in Rules language or Gateway policies, test patterns with the tools recommended in WAF FAQ and Gateway policy pages (Rust-oriented checking aligns with Cloudflare’s Rust regex engine).

  3. After saving, trigger a request that should match and one that should not; confirm the rule’s action (block, redirect, cache setting, etc.) using the product’s event or analytics views where available—for example Firewall event data for WAF rules.

  4. If you see ruleset exceeds complexity constraints on network rules, split the expression into multiple ordered rules per Form expressions guidance and retest.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about, and we will correct or clarify it.