Rewrite Rules
Rewrite Rules allow you to transform incoming URLs before any other processing happens. They work like nginx rewrite rules, enabling URL normalization, legacy URL support, and clean URL structures.
What are Rewrite Rules?
Rewrite Rules modify the request URL based on pattern matching. They're evaluated early in the request flow, right after Access Rules and before Page Rules, ensuring that URL transformations happen before configuration rules evaluate the URL.
Key characteristics:
Execute second in the rule sequence (after Access Rules, before Page and Conditional Rules)
Transform URLs before cache lookup
nginx-style URL rewriting
Evaluated in position order (1, 2, 3...)
Stop after first match - once a rule matches, no further Rewrite Rules are evaluated
How Rewrite Rules Work
Rewrite Rules are evaluated in ascending position order (1 → 2 → 3...) for every incoming request. When a rule's URL pattern matches, the URL is rewritten to the target path and evaluation stops.
Position-Based Evaluation
Request: /old-path
↓
Rewrite Rule (Position 1) → Match /old-path? → Rewrite to /new-path → STOP
↓ (no match)
Rewrite Rule (Position 2) → Match? → Rewrite → STOP
↓ (no match)
Continue to Page Rules with original or rewritten URL...Important: Once a Rewrite Rule matches, no further Rewrite Rules are evaluated. The rewritten URL is then used by all subsequent rules (Page Rules, Conditional Rules).
URL Matching
Rewrite Rules use the same URL matching system as Page Rules. You can match URLs using three different matchers:
Matchers
Equals (=): Exact match
Matches Regex (~): Case-sensitive regex
Matches Regex Case-Insensitive (~*): Case-insensitive regex
URL vs URI
URL: The complete web address including protocol and domain
Example:
https://www.example.com/category/data.html
URI: Just the path without domain or protocol
Example:
/category/data.html
Rewrite Rules work with URIs (the path only).
When to Use Rewrite Rules
Use Rewrite Rules When:
✅ You need to support legacy URLs while restructuring your site
✅ You want clean, SEO-friendly URLs (e.g.,
/product/123instead of/product.php?id=123)✅ You're migrating from another platform and need to maintain old URL structures
✅ You need URL normalization (e.g., remove trailing slashes, lowercase URLs)
✅ URL transformations should happen before other rules evaluate
Use Cases & Examples
Example 1: Legacy URL Support
Scenario: You've restructured your site from /old-section/page.html to /new-section/page, but need to support old links.
Why rewrite instead of redirect? Rewrites are transparent - the user doesn't see the URL change, and you maintain the original request context. Page Rules can then apply configurations based on the new URL.
Example 2: Clean URL Structure
Scenario: Your application uses query parameters, but you want clean URLs for SEO.
Why this works: Users and search engines see clean URLs (/product/12345), but your backend receives the query parameter format it expects.
Example 3: Platform Migration
Scenario: Migrating from WordPress to a new platform and need to support old post URLs.
Combined with Page Rules: After the rewrite, a Page Rule matching /posts/* can apply specific caching or optimization settings.
Target Path
The Target Path is where the URL gets rewritten to. It can be:
Static path:
/new-locationWith capture groups:
/section/$1(uses regex capture groups from the URL pattern)Multiple captures:
/category/$1/item/$2
Important: The target path is the actual backend path your application will receive. Make sure it's a valid path your application can handle.
Rule Order Best Practices
Since Rewrite Rules stop on first match, order them carefully:
1. Most Specific First
2. Exact Matches Before Patterns
3. Consider Downstream Rules
Remember that Page Rules and Conditional Rules will evaluate against the rewritten URL, not the original:
4. Consolidate Similar Patterns
Use regex to handle multiple cases efficiently:
Use regex for flexibility:
Uniqueness
Each site must have unique:
Rule names - each rule must have a unique name
URL patterns - you cannot have duplicate URL patterns
If you try to create a duplicate, you'll see: "Name already exists" or "Same matcher exists."
Common Mistakes
❌ Catch-all rewrite at position 1
✅ Specific patterns first
❌ Forgetting about Page Rules
✅ Page Rules match rewritten URLs
❌ Invalid target path
✅ Valid capture groups
Testing Rewrite Rules
Enable Debug Headers in your site configuration
Make test requests with the original URLs
Check response headers to see which rules were applied and what the URL became
Verify Page Rules are matching the rewritten URL, not the original
Last updated
Was this helpful?