以下示例会根据请求的用户代理字符串,将使用移动设备的访问者重定向到不同主机名。
此静态重定向示例会将当前 zone(example.com)上来自移动用户的请求重定向到 m.example.com,且不保留原始 HTTP 请求中的 URI 路径。
When incoming requests match(当传入请求匹配时)
- 在 Expression Editor 中输入以下表达式:
not http.host in {"m.example.com"} and (http.user_agent contains "mobi" or http.user_agent contains "Mobi")
Then(然后)
- Type: Static
- URL:
m.example.com - Status code: 301
有关此示例的说明:
not http.host in {"m.example.com"}条件可防止重定向循环。- 用户代理条件遵循 Mozilla 的建议 ↗,用于识别移动设备。
- Then(然后) > URL 的值应与你在规则筛选表达式的
http.host条件中输入的值相同。 - 你可以将用户重定向到 Cloudflare 上的其他 zone,或重定向到不在 Cloudflare 上的其他主机名。
此单次重定向示例会将当前 zone(example.com)上来自移动用户的请求重定向到 m.example.com,并保留原始 HTTP 请求的 URI 路径。
When incoming requests match(当传入请求匹配时)
- 在 Expression Editor 中输入以下表达式:
not http.host in {"m.example.com"} and (http.user_agent contains "mobi" or http.user_agent contains "Mobi")
Then(然后)
- Type: Dynamic
- Expression:
concat("https://m.example.com", http.request.uri.path) - Status code: 301
有关此示例的说明:
not http.host in {"m.example.com"}条件可防止重定向循环。- 用户代理条件遵循 Mozilla 的建议 ↗,用于识别移动设备。
- Then(然后) > Expression(表达式) 中的主机名应与你在规则筛选表达式的
http.host条件中输入的值相同。 - 根据你的用例,你可能希望启用 Then(然后) > Preserve query string(保留查询字符串),以同时保留原始请求的查询字符串。
- 你可以将用户重定向到 Cloudflare 上的其他 zone,或重定向到不在 Cloudflare 上的其他主机名。