Redirect Domain to Another Domain Cloudflare

Learn how to redirect a domain to another domain using Cloudflare, a powerful technique for managing domain migrations or consolidating multiple domains. This is useful for:

  • 301 redirecting inbound links after a domain name change (e.g., from example.net to example.com) to preserve SEO and avoid 404 errors.
  • Pointing multiple domains (e.g., .com, .net, .org) to a single domain (e.g., example.com) for a unified online presence.

1. Set Up DNS in Cloudflare

Add the domain (e.g., olddomain.com) to Cloudflare DNS, and create two A records:

  • Type: A, Name: @, Content: Your server’s IP
  • Type: A, Name: www, Content: Your server’s IP

Ensure both are proxied (orange cloud) so Cloudflare can handle the redirect.

2. Create a Redirect Rule

Go to Rules > Overview > Create rule > Redirect rule.

Rule name: Redirect to newdomain.com

Option 1: All incoming requests

Choose this option when you want all traffic, regardless of subdomain, to redirect to the new domain – ideal for a complete domain migration where no subdomains need to remain active.

It will redirect all proxied traffic, including all subdomains with A records (e.g., olddomain.com, www.olddomain.com, sub.olddomain.com), to newdomain.com.

Set “If incoming requests match…” to: All incoming requests

Option 2: Redirect Only www and non-www

Use this when you need to preserve specific subdomains (e.g., mail.olddomain.com for email services) while redirecting the main domain and www to the new domain.

It will redirect only olddomain.com and www.olddomain.com to newdomain.com, leaving other subdomains (e.g., mail.olddomain.com) unaffected.

Set “If incoming requests match…” to: Custom filter expression

Field: Hostname
Operator: Equals
Value: olddomain.com

Click [Or] and add:
Field: Hostname
Operator: Equals
Value: www.olddomain.com

3. Configure the Redirect

When setting up the redirect, we want to ensure a seamless transition for users and search engines alike. A dynamic redirect preserves the URL path – for example, if a user visits olddomain.com/some/path, they’ll be redirected to newdomain.com/some/path, not just the homepage. This maintains deep links, ensuring users land on the intended page, and helps with SEO by keeping the URL structure intact. We’ll also use a 301 status code to indicate a permanent redirect, telling search engines like Google to transfer all link equity (SEO value) to the new domain, preserving rankings and backlinks.

Url Redirect
Type: Dynamic
Expression: concat(“https://newdomain.com”, http.request.uri.path)
Status Code: 301
Check Preserve query string

Set Select order: First
Save and deploy the rule.

Conclusion

You’ve now set up a redirect from olddomain.com to newdomain.com using Cloudflare, ensuring a smooth migration for users and search engines. The dynamic redirect preserves URL paths, and the 301 status code secures your SEO rankings. To verify it’s working, visit olddomain.com and www.olddomain.com – they should redirect to newdomain.com. You can also use curl -I https://olddomain.com to check the HTTP status (should return 301).

If you used Option 2, check mail.olddomain.com to confirm it remains unaffected. Note that DNS propagation may take a few minutes, so if the redirect doesn’t work immediately, wait and clear your DNS cache (ipconfig /flushdns on Windows).

Categories: Cloud