跳到主要内容

页面规则

页面规则(Page Rules)是 Cloudflare 的核心功能之一,允许你针对特定 URL 设置自定义行为,如重定向、缓存控制、安全设置等。

页面规则基础

什么是页面规则?

页面规则允许你为匹配特定 URL 模式的请求设置自定义行为。每条规则包含:

  • URL 匹配模式
  • 一个或多个设置项

规则限制

计划页面规则数量
Free3 条
Pro20 条
Business100 条
Enterprise不限

注意:Free 计划只有 3 条规则,需要合理规划使用。

URL 匹配模式

基本语法

模式说明示例
*匹配任意字符example.com/*
*匹配任意路径段example.com/*/page
精确匹配不含通配符example.com/blog

匹配示例

匹配所有请求

example.com/*

匹配特定路径

example.com/blog/*

匹配特定文件类型

example.com/*.jpg

匹配带参数的 URL

example.com/search?q=*

匹配多个子域名

*.example.com/*

匹配优先级

规则按从上到下的顺序匹配,第一条匹配的规则生效。你可以在控制台拖动规则调整顺序。

可用设置项

转发 URL

将匹配的 URL 重定向到另一个地址:

URL 匹配: example.com/old-page
设置: Forwarding URL (301 - Permanent Redirect)
目标 URL: https://example.com/new-page

重定向类型

  • 301:永久重定向,SEO 友好
  • 302:临时重定向

缓存级别

控制缓存行为:

级别说明
Cache Everything缓存所有内容
Bypass绕过缓存
Standard标准缓存
Aggressive激进缓存

示例:缓存 API 响应

URL 匹配: example.com/api/data
设置: Cache Level: Cache Everything
Edge Cache TTL: 1 hour

边缘缓存 TTL

设置 Cloudflare 边缘节点的缓存时间:

URL 匹配: example.com/static/*
设置: Edge Cache TTL: 1 month

浏览器缓存 TTL

设置浏览器的缓存时间:

URL 匹配: example.com/assets/*
设置: Browser Cache TTL: 1 year

SSL 模式

为特定 URL 设置 SSL 模式:

URL 匹配: example.com/api/*
设置: SSL: Full (Strict)

安全级别

调整安全级别:

URL 匹配: example.com/admin/*
设置: Security Level: High

浏览器完整性检查

检查浏览器是否合法:

URL 匹配: example.com/*
设置: Browser Integrity Check: On

开发模式

临时禁用缓存:

URL 匹配: example.com/preview/*
设置: Development Mode: On

禁用性能功能

禁用特定性能优化:

URL 匹配: example.com/api/*
设置:
- Auto Minify: Off
- Rocket Loader: Off
- Mirage: Off

常用规则示例

示例一:域名重定向

www 重定向到非 www

URL 匹配: www.example.com/*
设置: Forwarding URL (301)
目标 URL: https://example.com/$1

注意$1 代表第一个通配符捕获的内容。

示例二:强制 HTTPS

将 HTTP 重定向到 HTTPS:

URL 匹配: example.com/*
设置: Always Use HTTPS: On

更简单的方法:在 SSL/TLS 设置中开启 "Always Use HTTPS"。

示例三:缓存静态资源

URL 匹配: example.com/static/*
设置:
- Cache Level: Cache Everything
- Edge Cache TTL: 1 month
- Browser Cache TTL: 1 year

示例四:绕过管理后台缓存

URL 匹配: example.com/admin/*
设置:
- Cache Level: Bypass
- Security Level: High

示例五:保护敏感页面

URL 匹配: example.com/login
设置:
- Security Level: High
- Browser Integrity Check: On
- Cache Level: Bypass

示例六:禁用 API 的性能优化

URL 匹配: example.com/api/*
设置:
- Auto Minify: Off
- Rocket Loader: Off
- Cache Level: Bypass

配置重定向规则

简单重定向

将旧页面重定向到新页面:

URL 匹配: example.com/old-blog/*
设置: Forwarding URL (301)
目标 URL: https://example.com/blog/$1

域名迁移

将整个域名重定向到新域名:

URL 匹配: old-domain.com/*
设置: Forwarding URL (301)
目标 URL: https://new-domain.com/$1

HTTPS 重定向

URL 匹配: http://example.com/*
设置: Forwarding URL (301)
目标 URL: https://example.com/$1

注意:如果开启了 "Always Use HTTPS",这个规则可能不需要。

高级用法

使用多个设置

一条规则可以包含多个设置:

URL 匹配: example.com/app/*
设置:
- Cache Level: Cache Everything
- Edge Cache TTL: 1 hour
- Browser Cache TTL: 4 hours
- Security Level: Medium
- SSL: Full (Strict)

规则优先级

规则按顺序匹配,建议:

  • 将更具体的规则放在前面
  • 将通用规则放在后面

示例

1. example.com/admin/*    (具体规则)
2. example.com/api/* (具体规则)
3. example.com/* (通用规则)

与其他规则配合

页面规则可以与以下功能配合:

  • Cache Rules
  • Redirect Rules
  • WAF 规则
  • Workers

配置步骤

创建页面规则

  1. 进入 "Rules" → "Page Rules"
  2. 点击 "Create Page Rule"
  3. 输入 URL 匹配模式
  4. 添加设置项
  5. 点击 "Save and Deploy"

编辑规则

  1. 点击规则右侧的 "Edit"
  2. 修改设置
  3. 保存

删除规则

  1. 点击规则右侧的 "Delete"
  2. 确认删除

调整规则顺序

在规则列表中,拖动规则到合适的位置。

替代方案

Redirect Rules

对于简单的重定向,可以使用 Redirect Rules:

Rules → Redirect Rules

Redirect Rules 更灵活,支持更多条件,且不占用 Page Rules 配额。

Cache Rules

对于缓存配置,可以使用 Cache Rules:

Caching → Cache Rules

Cache Rules 提供更精细的缓存控制,推荐用于缓存配置。

Configuration Rules

对于通用配置,可以使用 Configuration Rules:

Rules → Configuration Rules

Configuration Rules 可以设置更多选项,且不占用 Page Rules 配额。

常见问题

规则不生效怎么办?

  1. 检查 URL 匹配模式是否正确
  2. 检查规则顺序
  3. 清除缓存后测试
  4. 检查是否有其他规则冲突

通配符如何使用?

* 匹配任意字符,包括路径分隔符 /

示例:

  • example.com/* 匹配 example.com/aexample.com/a/b/c
  • example.com/*/page 匹配 example.com/blog/pageexample.com/news/page

如何捕获通配符内容?

在目标 URL 中使用 $1$2 等引用捕获的内容:

URL 匹配: example.com/blog/*/page/*
目标 URL: https://example.com/posts/$1/$2

$1 是第一个 * 匹配的内容,$2 是第二个 * 匹配的内容。

参考链接

下一步

完成页面规则配置后,接下来学习 Workers,了解如何在边缘运行无服务器代码。