Mastering Domain Redirects in Cloudflare: A Complete Guide
Learn how to properly redirect domains in Cloudflare, troubleshoot common issues like Error 522, and avoid DNS pitfalls. Real-world examples included.
Mastering Domain Redirects in Cloudflare: A Complete Guide
Domain redirects are one of the most common tasks when managing web infrastructure, whether you're consolidating brands, migrating to a new domain, or simply redirecting www to non-www sites. Cloudflare makes this powerful, but there are subtle gotchas that can leave you staring at Error 522 or DNS_PROBE_FINISHED_NXDOMAIN for hours.
In this guide, I'll walk you through exactly how to redirect domains in Cloudflare, cover the common mistakes that bite developers, and show you how to troubleshoot issues systematically. This comes from real-world experience redirecting bobadilla.work → bobadilla.tech in production.
What you'll learn:
- The correct way to set up domain redirects using Cloudflare Redirect Rules
- How DNS records interact with redirect rules (this is where most people fail)
- Troubleshooting Error 522, NXDOMAIN, and other redirect issues
- When to use Redirect Rules vs Page Rules vs Workers
- SEO-safe redirect strategies (301 vs 302)
- Common pitfalls and how to avoid them
Why Domain Redirects Matter
Before diving into implementation, let's understand when and why you need domain redirects.
Brand consolidation: You acquired a competitor or are rebranding — Example: oldcompany.com → newcompany.com
Domain standardization: Redirect www to apex or vice versa — Example: www.example.com → example.com
Multiple domains pointing to one site: You own several TLDs — Example: example.net, example.org → example.com
SEO migration: Preserving search rankings when changing domains
SEO Implications
301 (Permanent): Tells search engines this is permanent. Use this 95% of the time. Transfers 90-99% of link equity, search engines update their index, browser caches aggressively.
302 (Temporary): Tells search engines this is temporary. Does NOT transfer link equity, search engines keep indexing the old URL. Use for A/B tests or maintenance redirects.
Getting this wrong can tank your SEO — always use 301 for permanent moves.
The Cloudflare Redirect Architecture
Before configuring anything, understand how Cloudflare processes requests. Redirect Rules only fire if:
- DNS resolves (A/AAAA/CNAME record exists)
- Domain is proxied (orange cloud)
- Request matches the rule condition
Miss any of these? You get errors.
Method 1: Redirect Rules (Recommended)
This is the modern, scalable approach. Cloudflare is moving everything to Redirect Rules, and they're faster and more flexible than legacy Page Rules.
Step 1: Set Up DNS Records
Critical requirement: The source domain MUST resolve, even if you're redirecting everything away from it.
For the apex domain (example.com), create an A record with Name @, IPv4 192.0.2.1, and set it to Proxied.
Why `192.0.2.1`? This is a reserved "dummy" IP (RFC 5737) used for documentation/testing. It never routes anywhere — perfect placeholder when you don't need an actual origin.
Common mistake #1: Leaving the name as example.com instead of @. In Cloudflare DNS, @ = root/apex domain. If you type example.com, Cloudflare creates example.com.example.com → NXDOMAIN.
For the www subdomain, create an A record with Name www, IPv4 192.0.2.1, Proxied.
Common mistake #2: Forgetting the www record. If your redirect rule matches www.example.com but no DNS record exists, you get NXDOMAIN.
Step 2: Create the Redirect Rule
Navigate to: Rules → Redirect Rules → Create rule
Set a rule name, match on Hostname equals the old domain, then set Type to Dynamic Redirect with the expression concat("https://newcompany.com", http.request.uri.path), Status 301, and Preserve query string enabled.
Step 3: Test Thoroughly
Open an incognito window and test all variations:
# Apex domain
curl -I http://oldcompany.com
curl -I https://oldcompany.com
# WWW subdomain
curl -I http://www.oldcompany.com
curl -I https://www.oldcompany.com
# With paths
curl -I http://oldcompany.com/about
curl -I http://oldcompany.com/blog/post?id=123Expected output should show HTTP/2 301 with the correct location header.
Common mistake #3: Testing in the same browser without clearing cache. 301 redirects are cached hard by browsers. Use incognito/private mode or curl.
Troubleshooting Common Issues
Error: DNS_PROBE_FINISHED_NXDOMAIN
What it means: Domain doesn't resolve in DNS. Cause: Missing or incorrect DNS record.
Fix checklist:
- A record exists for
@(not the full domain name) - A record exists for
www(if redirecting www) - Records are saved
- Wait 30-60 seconds for DNS propagation
dig oldcompany.com
dig www.oldcompany.comError 522: Connection Timed Out
What it means: Cloudflare connected to your domain, but the redirect rule didn't fire, so it tried to reach the origin IP and timed out. Cause: Redirect rule not matching the request.
Fix checklist:
- DNS record is proxied (orange cloud)
- Redirect rule includes ALL hostnames (apex + www)
- Rule order is correct (should be first)
- Expression syntax is valid
Redirect Loop
What it means: Browser shows "Too many redirects"
Fix checklist:
- Check redirect rules on BOTH domains
- Ensure target domain has NO redirect rule pointing back
- Check
.htaccessor server config on target - Verify Cloudflare SSL mode (should be "Full" or "Full (strict)")
Real-World Example: bobadilla.work → bobadilla.tech
Requirements
- Redirect
bobadilla.work→bobadilla.tech - Redirect
www.bobadilla.work→bobadilla.tech - Preserve all paths and query strings
- Keep email working on bobadilla.work (MX records)
- SEO-safe (301 permanent)
DNS Configuration
# bobadilla.work DNS records
Type: A Name: @ IP: 192.0.2.1 Proxy: Proxied
Type: A Name: www IP: 192.0.2.1 Proxy: Proxied
Type: MX Name: @ Value: ... Proxy: DNS only
Type: TXT Name: @ Value: ... Proxy: DNS onlyKey point: Email records (MX, TXT) are NOT proxied. Only HTTP/HTTPS traffic goes through Cloudflare edge.
Redirect Rule Expression
(http.host eq "bobadilla.work" or http.host eq "www.bobadilla.work")Then: Type Dynamic, Expression concat("https://bobadilla.tech", http.request.uri.path), Status 301, Preserve query string.
Results
curl -I http://bobadilla.work
# → 301 → https://bobadilla.tech
curl -I http://bobadilla.work/projects
# → 301 → https://bobadilla.tech/projects
curl -I http://www.bobadilla.work/contact?source=email
# → 301 → https://bobadilla.tech/contact?source=emailBest Practices
1. Always Use 301 for Permanent Moves
Unless you have a specific reason (A/B test, temporary maintenance), always use 301. It transfers SEO value, signals permanence to search engines, and updates Google's index faster.
2. Preserve Path and Query Strings
Never redirect everything to the homepage. Use dynamic redirects with http.request.uri.path to preserve paths and http.request.uri.query for query strings.
3. Test All Variations
Don't assume. Test HTTP and HTTPS, apex and www, with and without trailing slashes, with query parameters.
4. Monitor After Launch
Use Cloudflare Analytics to verify redirect status codes (should see lots of 301s) and no 4xx or 5xx errors. Check Google Search Console — submit new sitemap and monitor index coverage.
5. Keep Email Records Separate
MX and TXT records should be DNS-only (grey cloud), NOT proxied through Cloudflare. Redirects only affect HTTP/HTTPS traffic.
Conclusion
Cloudflare domain redirects are powerful, fast, and free — when configured correctly.
Critical requirements:
- DNS must resolve (A record with orange cloud)
- Use
@for apex, not the full domain name - Include www in redirect rules if you use it
- Always test in incognito or curl
When to use what:
- Redirect Rules: 99% of cases (modern, unlimited, fast)
- Page Rules: Only if already using them (legacy)
- Workers: Complex logic, geo-routing, custom code
The redirect from bobadilla.work → bobadilla.tech took 5 minutes to configure once I understood these principles. Follow this guide and yours will too.
Have questions or hit a weird edge case? Email us at ale@bobadilla.tech. Need help with Cloudflare setup, migrations, or architecture? We offer consulting and implementation services — get in touch at bobadilla.tech.