If you need to redirect traffic from one subdomain to another, Cloudflare provides a simple and effective way to accomplish this using Page Rules or Redirect Rules. In this guide, we’ll walk you through setting up a redirect from https://old.example.com/*
to https://new.example.com/*
.
Why Use Cloudflare for Redirects?
- Easy to manage – No need to modify your web server configuration.
- Fast propagation – Cloudflare applies changes almost instantly.
- Performance optimization – Redirects handled at the Cloudflare edge reduce load on your server.
Method 1: Using Cloudflare Page Rules
Page Rules allow you to define how Cloudflare handles traffic for specific URLs. Here’s how to set up a redirect:
Step 1: Access Cloudflare Dashboard
- Log in to your Cloudflare account.
- Select your domain (example.com).
Step 2: Create a Page Rule
- Navigate to Rules > Page Rules.
- Click Create Page Rule.
- In the If the URL matches field, enter:
https://old.example.com/*
- Under Then the settings are, select Forwarding URL.
- Choose 301 (Permanent Redirect).
- In the Enter destination URL field, enter:
https://new.example.com/$1
(The$1
ensures that all paths fromold.example.com/*
are preserved when redirected tonew.example.com/*
.) - Click Save and Deploy.
Step 3: Test the Redirect
After saving the Page Rule, visit https://old.example.com/somepage
in your browser and confirm that it redirects to https://new.example.com/somepage
.
Method 2: Using Cloudflare Redirect Rules
Redirect Rules (under Transform Rules) provide more flexibility and scalability.
Step 1: Create a Redirect Rule
- In the Cloudflare dashboard, go to Rules > Redirect Rules.
- Click Create Rule.
- Under If the request matches, select:
- Hostname → Equals →
old.example.com
- Hostname → Equals →
- Under Then the settings are, choose Static Redirect.
- In the Redirect target, enter:
https://new.example.com/$1
- Choose 301 Permanent Redirect.
- Save and Deploy.
Step 2: Verify the Redirect
Try visiting https://old.example.com/somepage
to confirm it correctly redirects to https://new.example.com/somepage
.
Conclusion
Using Cloudflare Page Rules or Redirect Rules, you can easily set up domain redirects without modifying your server settings. This ensures a smooth transition for users while maintaining SEO benefits. If you’re managing multiple subdomains, Cloudflare’s rules make it simple to handle complex redirects efficiently.