如果您有一个别名域名仅用于将流量转发到另一个域名(即该域名没有关联的源服务器),可以直接在 Cloudflare 中设置重定向。
-
添加您的别名域名(例如
previous.com)到 Cloudflare。 -
确保您的别名域名具有已代理的 DNS A 或 CNAME 记录,能够正确解析 DNS 查询。您可能还需要为
www添加子域名 DNS 记录。对于
A记录,使用 IP 地址192.0.2.1。该地址不会将流量路由到源服务器,但允许 Cloudflare 对传入流量应用规则、重定向和 Workers。AAAA记录的等效 IP 地址为100::。Type Name IPv4 address Proxy status(代理状态) A @192.0.2.1Proxied A www192.0.2.1Proxied -
使用重定向规则将别名域名的流量转发到另一个域名。
以下示例将 smallshop.example.com 的所有请求通过 HTTPS 重定向到不同的主机名,并保留原始路径和查询字符串。
当传入请求匹配时
- Field: Hostname
- Operator: equals
- Value:
smallshop.example.com
如果您使用 Expression Editor,请输入以下表达式:
(http.host eq "smallshop.example.com")
Then
- Type: Dynamic
- Expression:
concat("https://globalstore.example.net", http.request.uri.path) - Status code: 301
- Preserve query string: Enabled
例如,重定向规则将执行以下重定向:
| Request URL | Target URL | Status code |
|---|---|---|
http://smallshop.example.com/ |
https://globalstore.example.net/ |
301 |
http://smallshop.example.com/admin/?logged_out=true |
https://globalstore.example.net/admin/?logged_out=true |
301 |
https://smallshop.example.com/?all_items=1 |
https://globalstore.example.net/?all_items=1 |
301 |
http://example.com/about/ |
(unchanged) | n/a |