cloudflare 301重定向规则设置

一图流

详细介绍



示例规则

以下部分包含单个重定向规则配置的示例。

将访问者重定向到特定页面的新 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.comhttps://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}/