将单个规则添加到现有规则集。使用此端点添加规则,而无需在请求中包含所有现有规则集规则。
使用以下 API 端点之一:
- 创建账户规则集规则
POST /accounts/{account_id}/rulesets/{ruleset_id}/rules - 创建区域规则集规则
POST /zones/{zone_id}/rulesets/{ruleset_id}/rules
在请求正文中包含规则定义。
默认情况下,该规则将添加到规则集中现有规则列表的末尾。要定义规则的特定位置,请根据更改规则集中规则的顺序中的指南,在请求正文中包含一个 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 WriteConfig Settings WriteDynamic URL Redirects WriteCache Settings WriteCustom Errors WriteOrigin WriteManaged headers WriteZone Transform Rules WriteMass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteHTTP DDoS Managed Ruleset WriteSanitize WriteTransform Rules WriteSelect Configuration WriteBot Management WriteZone WAF WriteAccount WAF WriteAccount Rulesets WriteLogs WriteLogs Write
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 时,索引为 n、n+1、n+2 等的现有规则将各向后移动一个位置——它们的新位置将为 n+1、n+2、n+3,依此类推。如果索引超出范围,该方法将返回400HTTP 状态码。
有关使用 position 对象的示例,请参阅更新规则集中的规则。