Page Rules for URL Forwarding & Redirects in Cloudflare
In this tutorial, you'll learn about Page Rules for URL Forwarding & Redirects in Cloudflare. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Cloudflare Page Rules for URL forwarding let you create 301 and 302 redirects using wildcard patterns, preserving query strings or replacing URL segments without modifying your origin server configuration.
What You'll Learn
By the end of this tutorial, you will understand how to create forwarding Page Rules for domain migrations, protocol upgrades, path restructures, and SEO-friendly URL redirects using Cloudflare's pattern matching.
Why It Matters
URL redirects are essential for site migrations, fixing broken links, and enforcing HTTPS. Cloudflare Page Rules handle redirects at the edge before requests reach your origin, reducing server load and improving response times.
Real-World Use
Doda Browser's ad-blocker uses pattern matching to redirect tracking URLs to local blocks. The same wildcard logic applies to Page Rules: redirect entire directory structures with a single rule instead of configuring redirects on your origin server.
Your Learning Path
flowchart LR
A[Page Rules Caching] --> B[Page Rules URL Forwarding]
B --> C[More Cloudflare Features]
B --> D{You Are Here}
style D fill:#f90,color:#fff
Forwarding URL Action Types
| Action | HTTP Status | Use Case |
|---|---|---|
| Forwarding URL | 301 or 302 | Permanent or temporary redirects with wildcard replacement |
| Always Use HTTPS | 301 | Redirect HTTP to HTTPS (automatic) |
| Automatic HTTPS Rewrites | - | Rewrite HTTP URLs in HTML to HTTPS |
Wildcard Replacement Syntax
| Pattern | Forward URL | Matches | Redirects To |
|---|---|---|---|
example.com/blog/* |
https://example.com/articles/$1 |
/blog/post-1 |
/articles/post-1 |
*example.com/* |
https://$1.example.com/$2 |
/page (from sub) |
sub.example.com/page |
example.com/*/product |
https://example.com/$1/item |
/shop/product |
/shop/item |
example.com/*.php |
https://example.com/$1 |
/about.php |
/about |
Creating a Forwarding Rule
- Log in to the Cloudflare dashboard.
- Select your domain.
- Go to Rules > Page Rules.
- Click Create Page Rule.
- Enter the URL pattern with wildcards.
- Choose Forwarding URL as the action.
- Select 301 Permanent or 302 Temporary.
- Enter the destination URL using
$1,$2references. - Save and deploy.
Common Redirect Scenarios
Scenario 1: Domain Migration
# Old domain to new domain with path preservation
# Pattern: *old-example.com/*
# Forward URL: https://new-example.com/$2
# Status: 301 Permanent Redirect
# Example: old-example.com/blog/post-1 -> new-example.com/blog/post-1
# The $2 captures everything after the domain.
Scenario 2: Directory Restructure
# Move all blog posts from /old-blog/ to /blog/
# Pattern: example.com/old-blog/*
# Forward URL: https://example.com/blog/$1
# Status: 301 Permanent Redirect
# Example: example.com/old-blog/hello-world -> example.com/blog/hello-world
Scenario 3: Remove File Extensions
# Convert .html URLs to clean URLs
# Pattern: *example.com/*.html
# Forward URL: https://$1.example.com/$2
# Status: 301 Permanent Redirect
# Example: example.com/about.html -> example.com/about
# Note: This requires a second rule to serve the actual .html file
# at the clean URL path.
Redirect with Query String Handling
// Cloudflare Worker for advanced query string redirects
export default {
async fetch(request) {
const url = new URL(request.url);
const params = url.searchParams;
// Redirect old campaign URLs to new structure
if (url.pathname === '/landing' && params.has('campaign')) {
const campaign = params.get('campaign');
const newUrl = `https://example.com/campaigns/${campaign}`;
return Response.redirect(newUrl, 301);
}
return fetch(request);
}
};
// Expected behavior:
// GET /landing?campaign=summer2026 -> 301 to /campaigns/summer2026
Testing Redirects
# Test a 301 redirect from the command line
curl -I https://old-example.com/blog/test-post
# Expected output:
# HTTP/2 301 Moved Permanently
# location: https://new-example.com/blog/test-post
# cf-page-rule: Page-Rule-Matched
# Test with verbose output to see redirect chain
curl -L -o /dev/null -w "%{url_effective}" https://old-example.com/blog/test-post
# Expected output:
# https://new-example.com/blog/test-post
Common Errors
Redirect loop. If the destination URL also matches the same Page Rule, the request bounces between the two URLs. Always ensure the destination URL does not match the source pattern.
Both HTTP and HTTPS rules conflicting. If you have separate rules for http://example.com/* and https://example.com/*, they may conflict. Use *example.com/* for protocol-agnostic matching with an Always Use HTTPS rule.
Wildcard $1 references incorrect segment. Each * in the pattern corresponds to a numbered $ reference. Count from left to right: first * is $1, second is $2, and so on.
Case sensitivity. Cloudflare Page Rule patterns are case-insensitive. If you need case-sensitive matching, use a Cloudflare Worker.
Too many individual redirect rules. Each unique redirect path requires its own rule. For bulk redirects (100+), use a Cloudflare Worker with a redirect map instead of Page Rules.
Practice Questions
- How does the $1 wildcard reference work in a forwarding URL destination?
- What is the difference between a 301 and a 302 redirect in the context of SEO?
- How can you test a Cloudflare Page Rule redirect using curl?
Challenge
Plan a domain Migration from oldsite.com to newsite.com. The old site has the following paths: /blog/*, /products/*, /about.html, and a wildcard catch-all. Write the minimum Page Rules needed to preserve SEO equity during the Migration.
Real-World Task
Audit your production site for broken links using a crawler tool. For each broken URL category, determine whether a single Page Rule with a wildcard pattern can fix all matches. Implement the redirect.
FAQ
Related Tutorials
- Page Rules Intro — URL pattern matching fundamentals
- Page Rules Caching — Combine redirects with cache control
- Cloudflare — Complete platform guide
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro — security-first tools for the modern web.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro