# Conditional Routing Rules

> **TL;DR:** You set rules that redirect visitors to different destination URLs based on conditions: visitor country, total click count on the link, or a date range. Rules are evaluated in priority order — the first matching rule wins. If no rule matches, the default destination is used.

Redirect visitors to different destinations based on their country, click thresholds, dates, or any combination of conditions — all on a per-link basis.

## What Are Conditional Rules

A conditional rule overrides a link's default destination URL when its conditions match. Each link can have multiple rules, evaluated in priority order. The first rule that matches wins. If no rules match, the visitor goes to the link's default target URL.

You configure conditional rules from within the link editor, under the **Conditional Routing Rules** section.

## Rule Fields

Each rule has the following fields:

| Field | Type | Description |
|-------|------|-------------|
| **name** | Text, optional (max 120 characters) | Descriptive label for the rule (e.g., "US visitors to US store") |
| **is_active** | Boolean (default: true) | Toggle the rule on or off without deleting it |
| **priority** | Integer, 0–100 (default: 100) | Evaluation order — lower numbers evaluate first |
| **match_mode** | `all` or `any` | How conditions combine: `all` = every condition must match (AND), `any` = at least one must match (OR) |
| **target_url** | URL | Alternate destination for visitors who match this rule |
| **redirect_type** | `301`, `302`, or `307` | HTTP redirect status code for this rule |
| **start_at** | Datetime (optional) | Rule activates after this date and time |
| **end_at** | Datetime (optional) | Rule deactivates after this date and time |
| **conditions** | Array (optional) | Condition objects (see below). If empty, the rule matches all visitors within its date window. |

## Condition Types

Each condition checks a specific attribute of the click against your defined criteria.

| Type | Description | Operators | Value Format |
|------|-------------|-----------|--------------|
| **total_clicks** | Total click count on the link (evaluated as what the count will be *after* the current click) | `gt`, `gte`, `eq`, `lt`, `lte` | Number |
| **country_in** | Visitor's country, resolved from GeoLite2 database or Cloudflare/CDN headers | `in` | Array of 2-letter ISO country codes (e.g., `["US", "CA"]`) |
| **date_after** | Current date is after a specified date | `gt`, `gte` | Date string (e.g., `"2026-04-01"`) |
| **date_before** | Current date is before a specified date | `lt`, `lte` | Date string (e.g., `"2026-06-30"`) |

### Operator Reference

| Operator | Meaning |
|----------|---------|
| `gt` | Greater than |
| `gte` | Greater than or equal to |
| `eq` | Equal to |
| `lt` | Less than |
| `lte` | Less than or equal to |
| `in` | Value is in the provided list |

## Match Modes

The match mode determines how multiple conditions within a single rule combine.

### All (AND Logic)

Every condition in the rule must be true for the rule to match.

**Example:** A rule with match mode `all` and two conditions — `country_in: ["US"]` and `total_clicks lt 500` — only matches visitors from the US when the link has fewer than 500 total clicks.

### Any (OR Logic)

At least one condition must be true for the rule to match.

**Example:** A rule with match mode `any` and two conditions — `country_in: ["US"]` and `country_in: ["CA"]` — matches visitors from either the US or Canada.

## Priority and Evaluation Order

Rules evaluate in ascending priority order. A rule with priority `10` evaluates before a rule with priority `100`. The first rule that matches determines the redirect destination.

### Evaluation Flow

1. Link Manager Pro loads all **active** rules for the link, sorted by priority (lowest first).
2. For each rule:
   - Check if the current date falls within the `start_at`/`end_at` window (if set). Skip the rule if outside the window.
   - Evaluate all conditions using the rule's match mode.
   - If the rule matches → redirect the visitor to this rule's `target_url` using this rule's `redirect_type`. Stop evaluating.
3. If no rule matches → redirect to the link's default `target_url`.

## Date Range Restrictions

Use `start_at` and `end_at` to limit when a rule is active, independent of its conditions.

| Field | Behavior |
|-------|----------|
| **start_at** only | Rule activates on that date and stays active indefinitely |
| **end_at** only | Rule is active immediately and deactivates on that date |
| **Both set** | Rule is active only between `start_at` and `end_at` |
| **Neither set** | Rule is always eligible for evaluation (conditions still apply) |

Date range checks happen before condition evaluation. If the current date is outside the range, the rule is skipped entirely.

## Creating a Rule

1. Go to **Link Manager → Pretty Links**.
2. Click a link to open the link editor.
3. Scroll to the **Conditional Routing Rules** section.
4. Click **Add Rule**.
5. Fill in the rule fields:
   - Enter a descriptive **name**.
   - Set the **priority** (lower = evaluated first).
   - Choose the **match mode** (`all` or `any`).
   - Enter the **target URL** for this rule's redirect.
   - Select the **redirect type** (301, 302, or 307).
   - Optionally set **start_at** and **end_at** dates.
6. Add one or more **conditions**:
   - Select the condition type.
   - Choose the operator.
   - Enter the value.
7. Click **Save**.

## Use Case Examples

### Geo-Targeting: Regional Storefronts

Redirect visitors to their regional store based on country.

**Setup:** Create two rules on the same link.

**Rule 1 — US Store (priority 10):**
- match_mode: `all`
- target_url: `https://us.store.com/product`
- redirect_type: `302`
- Condition: `country_in` → `in` → `["US"]`

**Rule 2 — EU Store (priority 20):**
- match_mode: `all`
- target_url: `https://eu.store.com/product`
- redirect_type: `302`
- Condition: `country_in` → `in` → `["DE", "FR", "IT", "ES", "NL"]`

Visitors from the US go to the US store. Visitors from listed EU countries go to the EU store. Everyone else goes to the link's default target URL.

### Click Cap: Redirect After a Threshold

Send visitors to a different page after a link reaches a click threshold.

**Rule — Redirect after 1000 clicks (priority 10):**
- match_mode: `all`
- target_url: `https://yoursite.com/sold-out`
- redirect_type: `302`
- Condition: `total_clicks` → `gte` → `1000`

The first 999 visitors go to the default destination. Starting at click 1000, visitors redirect to the sold-out page.

### Time-Limited Promotion

Redirect to a sale page only during a specific date range.

**Rule — Holiday sale (priority 10):**
- match_mode: `all`
- target_url: `https://yoursite.com/holiday-sale`
- redirect_type: `302`
- start_at: `2026-12-20 00:00:00`
- end_at: `2026-12-31 23:59:59`

No conditions needed — the date range restriction alone controls when this rule is active. Outside the date window, visitors go to the default destination.

### Combining Conditions: Geo + Date

Redirect US visitors to a Black Friday page during a specific window.

**Rule — US Black Friday (priority 10):**
- match_mode: `all`
- target_url: `https://yoursite.com/black-friday`
- redirect_type: `302`
- Condition 1: `country_in` → `in` → `["US"]`
- Condition 2: `date_after` → `gte` → `"2026-11-27"`
- Condition 3: `date_before` → `lte` → `"2026-11-30"`

Because match mode is `all`, all three conditions must be true. Only US visitors between November 27 and November 30 see the Black Friday page.

## Managing Rules

### Editing a Rule

1. Open the link editor.
2. Find the rule in the Conditional Routing Rules section.
3. Modify any fields or conditions.
4. Click **Save**.

### Deleting a Rule

1. Open the link editor.
2. Click the **Delete** button on the rule you want to remove.
3. Confirm the deletion.

### Reordering Rules

Change the **priority** value on each rule to control evaluation order. Lower numbers evaluate first. You do not need consecutive numbers — values like 10, 20, 30 work well and leave room for inserting rules later.

### Syncing Rules (Bulk Replace)

The sync operation performs an atomic replace of all rules on a link. It removes every existing rule and replaces them with a new set in a single operation. Use sync when you need to restructure all rules on a link at once rather than editing them individually.

## Troubleshooting

If a conditional rule is not triggering as expected, check each item in this list:

| Check | What to Verify |
|-------|----------------|
| **Rule is active** | Confirm `is_active` is set to true |
| **Priority order** | A higher-priority rule (lower number) may be matching first — review all rules on the link |
| **Date range** | If `start_at` or `end_at` is set, confirm the current date falls within the window |
| **Match mode** | If set to `all`, every condition must be true — check that all conditions can be satisfied simultaneously |
| **Country detection** | Country code relies on GeoLite2 or Cloudflare/CDN headers — verify your server provides this data |
| **Click count timing** | `total_clicks` evaluates against the count *after* the current click, not before — a `gte 1000` condition triggers on the 1000th click |
| **Condition values** | Double-check country codes are valid 2-letter ISO codes and date strings are formatted correctly |
| **Link target** | Confirm the rule's `target_url` is a valid, accessible URL |

If no rules match, the link uses its default target URL. This is expected behavior, not an error.

---

**Step-by-step guide:** [How to Create Redirect Links in WordPress](https://docs.linkmanagerpro.com/guides/redirect-links)
