跳转到内容
搜索文档

将规则添加到规则集

最后更新 查看 MarkdownAgent 设置

将单个规则添加到现有规则集。使用此端点添加规则,而无需在请求中包含所有现有规则集规则。

使用以下 API 端点之一:

在请求正文中包含规则定义。

默认情况下,该规则将添加到规则集中现有规则列表的末尾。要定义规则的特定位置,请根据更改规则集中规则的顺序中的指南,在请求正文中包含一个 position 对象。

调用此方法将创建规则集的新版本。

示例

以下 POST 请求将规则添加到区域 $ZONE_ID 的规则集 $RULESET_ID 中。规则集 ID 以前使用列出区域规则集操作获取,并对应于 http_request_firewall_custom 阶段的入口点规则集。

响应将包含添加规则后的完整规则集。

Required API token permissions

At least one of the following token permissions is required:
  • Response Compression Write
  • Config Settings Write
  • Dynamic URL Redirects Write
  • Cache Settings Write
  • Custom Errors Write
  • Origin Write
  • Managed headers Write
  • Zone Transform Rules Write
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • HTTP DDoS Managed Ruleset Write
  • Sanitize Write
  • Transform Rules Write
  • Select Configuration Write
  • Bot Management Write
  • Zone WAF Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
  • Logs Write
Create a zone ruleset rulebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"action": "js_challenge",
		"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
		"description": "challenge GB and FR based on bot score"
	}'
{
	"result": {
		"id": "<RULESET_ID>",
		"name": "Zone Ruleset 1",
		"description": "My phase entry point ruleset at the zone level",
		"kind": "zone",
		"version": "11",
		"rules": [
			{
				"id": "<RULE_ID_1>",
				"version": "1",
				"action": "challenge",
				"expression": "not http.request.uri.path matches \"^/api/.*$\"",
				"last_updated": "2023-11-23T11:36:24.192361Z",
				"ref": "<RULE_REF_1>",
				"enabled": true
			},
			{
				"id": "<NEW_RULE_ID>",
				"version": "1",
				"action": "js_challenge",
				"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
				"description": "challenge GB and FR based on bot score",
				"last_updated": "2024-06-22T12:35:58.144683Z",
				"ref": "<NEW_RULE_REF>",
				"enabled": true
			}
		],
		"last_updated": "2024-06-22T12:35:58.144683Z",
		"phase": "http_request_firewall_custom"
	},
	"success": true,
	"errors": [],
	"messages": []
}

定义规则集中的规则位置

要在规则集中定义新规则的位置,请在请求中包含 position 对象,其中包含以下内容之一:

  • "before": "<RULE_ID>" — 将规则放置在规则 <RULE_ID> 之前。使用此参数并将规则 ID 值设为空(""),可将该规则设为规则集中的第一条规则。

  • "after": "<RULE_ID>" — 将规则放置在规则 <RULE_ID> 之后。使用此参数并将规则 ID 值设为空(""),可将该规则设为规则集中的最后一条规则。

  • "index": <POSITION_NUMBER> — 将规则放置在整数 <POSITION_NUMBER> 所指定的确切位置。位置编号从 1 开始。规则集中从指定位置编号开始的现有规则将向后移动一个位置(不会覆盖任何规则)。例如,当您使用 index 将规则放在位置 n 时,索引为 nn+1、n+2 等的现有规则将各向后移动一个位置——它们的新位置将为 n+1、n+2、n+3,依此类推。如果索引超出范围,该方法将返回 400 HTTP 状态码。

有关使用 position 对象的示例,请参阅更新规则集中的规则

这篇文档对您有帮助吗?