* Single Redirects — Example rules · Cloudflare Rules docs - Cloudflare Docs
* [https://developers.cloudflare.com/rules/url-forwarding/single-redirects/examples/](https://developers.cloudflare.com/rules/url-forwarding/single-redirects/examples/)
* The following sections contain example single redirect rule configurations.
* 2024-06-06 21:36:14示例规则
以下部分包含单个重定向规则配置的示例。
将访问者重定向到特定页面的新 URL
此区域示例静态重定向example.com将把请求/contact-us/页面的访问者重定向到新页面 URL /contacts/。
当传入请求匹配时
- 字段: URI 路径
- 运算符: 等于
- **价值:/contact-us/
如果您使用表达式编辑器,请输入以下表达式:
http.request.uri.path eq "/contact-us/"
然后**
- 类型: 静态
- **网址:**/contacts/
- 状态代码: 301
- 保留查询字符串: 已启用
例如,重定向规则将执行以下重定向:
| 请求 URL | 目标网址 | 状态代码 | 
|---|---|---|
|  example.com/contact-us/ |  example.com/contacts/ |  301 | 
|  example.com/contact-us/?state=TX |  example.com/contacts/?state=TX |  301 | 
|  example.com/team/ | (未变) | 无 | 
将所有请求重定向到不同的主机名(主要就这个)
此示例动态重定向将使用 HTTPS 将所有请求重定向smallshop.example.com到不同的主机名,保留原始路径和查询字符串。
当传入请求匹配时
- 字段: 主机名
- 运算符: 等于
- **价值:smallshop.example.com
如果您使用表达式编辑器,请输入以下表达式:
(http.host eq "smallshop.example.com")
然后**
- 类型: 动态
- **表达:**concat("https://globalstore.example.net", http.request.uri.path)
- 状态代码: 301
- 保留查询字符串: 已启用
例如,重定向规则将执行以下重定向:
| 请求 URL | 目标网址 | 状态代码 | 
|---|---|---|
|  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/ | (未变) | 无 | 
将管理区域请求重定向到 HTTPS
此区域示例动态重定向example.com将把特定子域 ( store.example.com) 的管理区域的请求重定向到 HTTPS,保留原始路径和查询字符串。
当传入请求匹配时
- 字段: SSL/HTTPS
- 值: 关闭
和
- 字段: 主机名
- 运算符: 等于
- **价值:**store.example.com
和
- 字段: URI 路径
- 操作符: 以...开头
- **价值:/admin
如果您使用表达式编辑器,请输入以下表达式:
(not ssl and http.host eq "store.example.com" and starts_with(http.request.uri.path, "/admin"))
然后**
- 类型: 动态
- **表达:**concat("https://", http.host, http.request.uri.path)
- 状态代码: 301
- 保留查询字符串: 已启用
该规则包括SSL/HTTPS:关闭(not ssl在规则表达式中)以避免重定向循环。
例如,重定向规则将执行以下重定向:
| 请求 URL | 目标网址 | 状态代码 | 
|---|---|---|
|  http://store.example.com/admin/products/ |  https://store.example.com/admin/products/ |  301 | 
|  https://store.example.com/admin/products/ | (未变) | 无 | 
|  http://store.example.com/admin/?logged_out=true |  https://store.example.com/admin/?logged_out=true |  301 | 
|  http://store.example.com/?all_items=true | (未变) | 无 | 
|  http://example.com/admin/ | (未变) | 无 | 
将英国和法国访客重定向至其特定子域名
此区域示例动态重定向将分别example.com将请求网站根路径 ( ) 的英国和法国访问者重定向/到其本地化子域https://gb.example.com和https://fr.example.com。
当传入请求匹配时
使用表达式编辑器:
(ip.geoip.country eq "GB" or ip.geoip.country eq "FR") and http.request.uri.path eq "/"
然后
- 类型: 动态
- **表达:**lower(concat("https://", ip.geoip.country, ".example.com"))
- 状态代码: 301
例如,重定向规则将执行以下重定向:
| 参观者国家 | 请求 URL | 目标网址 | 状态代码 | 
|---|---|---|---|
| 英国 |  example.com |  https://gb.example.com |  301 | 
| 法国 |  example.com |  https://fr.example.com |  301 | 
| 美国 |  example.com | (未变) | 无 | 
从 URL 路径中删除语言环境信息
此区域动态重定向示例example.com将把访问者从包含区域设置的旧 URL 格式(例如/en-us/<page_name>)重定向到新格式/<page_name>。
当传入请求匹配时
- 字段: URI 路径
- 运算符: 匹配正则表达式
- **价值:^/[A-Za-z]{2}-[A-Za-z]{2}/
如果您使用表达式编辑器,请输入以下表达式:
http.request.uri.path matches "^/[A-Za-z]{2}-[A-Za-z]{2}/"
然后**
- 类型: 动态
- **表达:**regex_replace(http.request.uri.path, "^/[A-Za-z]{2}-[A-Za-z]{2}/(.*)", "/${1}")
- 状态代码: 301
- 保留查询字符串: 已启用
该函数regex\_replace()允许您使用正则表达式的捕获组提取 URL 的部分内容。通过将正则表达式的一部分放在括号中来创建捕获组。然后,${<num>}在替换字符串中使用 引用捕获组,其中<num>是捕获组的编号。
例如,重定向规则将执行以下重定向:
| 请求 URL | 目标网址 | 状态代码 | 
|---|---|---|
|  example.com/en-us/meet-our-team |  example.com/meet-our-team |  301 | 
|  example.com/pt-BR/meet-our-team |  example.com/meet-our-team |  301 | 
|  example.com/en-us/calendar?view=month |  example.com/calendar?view=month |  301 | 
|  example.com/meet-our-team | (未变) | 无 | 
|  example.com/robots.txt | (未变) | 无 | 
执行移动重定向
以下示例将根据请求用户代理字符串将使用移动设备的访问者重定向到不同的主机名。
重定向移动用户并删除原始 URI 路径
example.com此示例静态重定向将把移动用户对当前区域 () 的请求重定向到,m.example.com而不保留原始 HTTP 请求中的 URI 路径。
当传入请求匹配时
- 在表达式编辑器中输入以下表达式:
 not http.host in {"m.example.com"} and (http.user_agent contains "mobi" or http.user_agent contains "Mobi")
然后
- 类型: 静态
- **网址:**m.example.com
- 状态代码: 301
关于此示例的注释:
- 该not http.host in {"m.example.com"}条件可防止重定向循环。
- 用户代理条件遵循Mozilla 的建议用于识别移动设备。
- Then > URL值应该与您在http.host规则过滤表达式的条件中输入的值相同。
- 您可以将用户重定向到 Cloudflare 上的其他区域或 Cloudflare 上以外的其他主机名。
重定向移动用户,保持原始路径
example.com此示例动态重定向将把移动用户对当前区域 () 的请求重定向到m.example.com,并保留原始 HTTP 请求的 URI 路径。
当传入请求匹配时
- 在表达式编辑器中输入以下表达式:
 not http.host in {"m.example.com"} and (http.user_agent contains "mobi" or http.user_agent contains "Mobi")
然后
- 类型: 动态
- **表达:**concat("https://m.example.com", http.request.uri.path)
- 状态代码: 301
关于此示例的注释:
- 该not http.host in {"m.example.com"}条件可防止重定向循环。
- 用户代理条件遵循Mozilla 的建议用于识别移动设备。
- Then > Expression中的主机名应该与您在http.host规则过滤表达式的条件中输入的主机名相同。
- 根据您的使用情况,您可能需要启用Then >保留查询字符串以保留原始请求的查询字符串。
- 您可以将用户重定向到 Cloudflare 上的其他区域或 Cloudflare 上以外的其他主机名。
评论区