cloudflare页面规则实现direct

如果你是在 Cloudflare(不是 Cloudware)中定义 URL 重写规则,可以通过 “规则(Rules)” 功能来实现。


方法 1:使用 重写规则(Transform Rules)

Cloudflare 支持使用「Transform Rules → URL Rewrite」功能对请求 URL 进行重写(Rewrite Path)。

1.1.使用路径重写的场景示例

例如将请求:

https://example.com/old-path/123

重写为:

https://example.com/new-path/123

设置步骤如下:

  1. 登录 Cloudflare 控制台。

  2. 进入你的网站域名。

  3. 左侧导航点击 RulesTransform Rules

  4. 创建规则,选择类型为 URL Rewrite

  5. 设置匹配条件,例如:

    字段:URI Path  | 运算符:starts with | 值:/old-path/
    
  6. 设置重写目标,例如:

    Rewrite to: /new-path/${uri.path[1]}
    

方法 2:使用 页面规则(Page Rules)

这个适用于简单的 URL 重定向,比如 301/302 重定向,不是严格意义上的“重写”。

2.1.将 /old 重定向到 /new

  1. 进入网站的 Cloudflare 控制台。

  2. 点击左侧菜单 RulesPage Rules

  3. 创建规则:

    • URL 匹配:https://example.com/old*
    • 设置:Forwarding URL (301)https://example.com/new

小提示

  • Rewrite是服务端内部改写路径,用户浏览器不会看到 URL 变更;
  • Redirect是用户浏览器会跳转到新 URL(301/302);
  • Transform Rules 推荐用于 URL 重写
  • Page Rules 推荐用于重定向