相关背景
Cloudflare Redirect Rules 是 Cloudflare 提供的一项功能,允许网站管理员设置和管理 URL 重定向规则。这些规则可以自动将访问者从一个 URL 重定向到另一个 URL,无需修改源服务器的配置。
应用场景
- 域名迁移:当网站更换域名时,使用 301 重定向将旧域名的页面指向新域名,以保持流量和 SEO 权重。
- HTTP 转 HTTPS:为了提高安全性,可以将所有 HTTP 请求重定向到 HTTPS 版本。这不仅保护用户数据,还能提高搜索引擎排名。
操作指南
print("test")
- 首先将旧域名托管到 Cloudflare 并开启 Proxy,由于重定向会先执行,因此这里可以指向任意ip,这里以
2.2.2.2
为例。

- 来到 Redirect Rules 页面,点击Create rule

如果第一步的域名托管没有完成这里会进行风险提示
规则配置 – 域名迁移
When incoming requests match
* Wildcard pattern
* Request URL: http*://smallshop.example.com/*
Then
* Target URL: https://globalstore.example.net/${2}
* Status code: 301
* Preserve query string: Enabled
For example, the redirect rule would perform the following redirects:
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 |

规则配置 – HTTP 转 HTTPS
When incoming requests match
* Wildcard pattern
* Request URL: http://store.example.com/admin*
Then
* Target URL: https://store.example.com/${1}
* Status code: 301
* Preserve query string: Enabled
For example, the redirect rule would perform the following redirects:
Request URL | Target URL | Status code |
---|---|---|
http://store.example.com/admin/products/ | https://store.example.com/admin/products/ | 301 |
https://store.example.com/admin/products/ | (unchanged) | n/a |
http://store.example.com/admin/?logged_out=true | https://store.example.com/admin/?logged_out=true | 301 |
http://store.example.com/?all_items=true | (unchanged) | n/a |
http://example.com/admin/ | (unchanged) |
