如何用Cloudflare实现带相同query parameters的域名跳转(无需独立主机)
Absolutely! You can pull this off entirely with Cloudflare—no dedicated hosting required for mydomain1.com, since all the redirection logic happens at Cloudflare's edge nodes. Here are two reliable methods to set this up:
Method 1: Use Cloudflare Redirect Rules (Recommended)
Redirect Rules are the modern, flexible way to handle this, and they work across most Cloudflare plans:
- First, make sure
mydomain1.comis added to your Cloudflare account, and its DNS records are pointing to Cloudflare (you can use a dummy A record pointing to192.0.2.1—a reserved test IP—since you don't need an actual host). - Navigate to your
mydomain1.comdashboard in Cloudflare, then go to Rules > Redirect Rules. - Click Create Rule:
- Give your rule a descriptive name (e.g., "Full Path + Query Redirect to mydomain2").
- Under When incoming requests match, set the condition to Host equals www.mydomain1.com (adjust if you want to redirect the entire root domain too).
- Under Then, select Redirect as the action.
- Choose Dynamic as the redirect type (this lets us reuse the original path and query parameters).
- For the target URL, enter:
https://www.mydomain2.com${request.path}${request.query} - Pick a status code: use 301 (Permanent Redirect) if this is a permanent change (better for SEO), or 302 (Temporary Redirect) if you might revert it later.
- Save the rule, and it should take effect within a few minutes.
Method 2: Use Cloudflare Page Rules (Legacy but Still Functional)
If you prefer the older Page Rules interface or are on a plan that supports it:
- Go to Rules > Page Rules in your
mydomain1.comCloudflare dashboard. - Click Create Page Rule:
- Enter the URL pattern:
www.mydomain1.com/*(the*captures any path after the domain). - Add a setting: select Forwarding URL > Permanent Redirect (301) (or Temporary if needed).
- For the destination URL, enter:
https://www.mydomain2.com/$1
- The
$1will insert the captured path from the original request, and Cloudflare automatically retains all query parameters by default—no extra setup needed here.
- Enter the URL pattern:
- Save the rule, and it’ll start working shortly.
Both methods will ensure that any request to www.mydomain1.com/somepath?somequery gets redirected to www.mydomain2.com/somepath?somequery with the exact same path and query string intact. And since all logic runs on Cloudflare’s edge, you don’t need to spin up any servers for mydomain1.com at all.
内容的提问来源于stack exchange,提问作者UtkarshPramodGupta

