对账户或 zone 级别规则集中的现有规则应用一项或多项更改。
使用以下任一 API 端点:
- Update an account ruleset rule
PATCH /accounts/{account_id}/rulesets/{ruleset_id}/rules/{rule_id} - Update a zone ruleset rule
PATCH /zones/{zone_id}/rulesets/{ruleset_id}/rules/{rule_id}
您可以更新规则定义(修改其字段),或更改规则在规则集中的顺序。调用此方法会创建规则集的新版本。
要更新规则定义,请在请求体中包含新的规则定义。您必须包含希望成为新规则定义一部分的所有规则字段,即使这些字段的值没有变化。
响应将包含更新该规则后的完整规则集。
Required API token permissions
At least one of the following token permissions is required:Mass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteTransform Rules WriteSelect Configuration WriteAccount WAF WriteAccount Rulesets WriteLogs Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID/rules/$RULE_ID_1" \
--request PATCH \
--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": "Custom Ruleset 1",
"description": "My first custom ruleset",
"kind": "custom",
"version": "11",
"rules": [
{
"id": "<RULE_ID_1>",
"version": "2",
"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": "2023-03-22T12:54:58.144683Z",
"ref": "<RULE_REF_1>",
"enabled": true
},
{
"id": "<RULE_ID_2>",
"version": "1",
"action": "challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"last_updated": "2022-11-23T11:36:24.192361Z",
"ref": "<RULE_REF_2>",
"enabled": true
}
],
"last_updated": "2023-03-22T12:54: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 状态码。
若只在 PATCH 请求体中包含 position 对象,即可在不更改规则定义的情况下重新排序。您也可以在同一次 PATCH 请求中同时包含 rule 对象和 position 对象,以同时更新规则定义并重新排序。
以下示例基于如下(简化)规则集:
{
"rules": [
{ "id": "<RULE_ID_1>" },
{ "id": "<RULE_ID_2>" },
{ "id": "<RULE_ID_3>" },
{ "id": "<RULE_ID_4>" }
]
}以下包含 position 对象的请求将规则 $RULE_ID_2 置于首位:
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/$RULE_ID_2" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"position": {
"before": ""
}
}'在此情况下,新的规则顺序为:
<RULE_ID_2>、<RULE_ID_1>、<RULE_ID_3>、<RULE_ID_4>
以下包含 position 对象的请求将规则 $RULE_ID_2 放在规则 3 之后:
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/$RULE_ID_2" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"position": {
"after": "<RULE_ID_3>"
}
}'在此情况下,新的规则顺序为:
<RULE_ID_1>、<RULE_ID_3>、<RULE_ID_2>、<RULE_ID_4>
以下包含 position 对象的请求将规则 $RULE_ID_1 放在位置 3,使其成为规则集中的第三条规则:
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/$RULE_ID_1" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"position": {
"index": 3
}
}'在此情况下,新的规则顺序为:
<RULE_ID_2>、<RULE_ID_3>、<RULE_ID_1>、<RULE_ID_4>