アカウントまたはゾーン単位で、ルールセット内の既存ルールに 1 つ以上の変更を適用します。
次のいずれかの 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から始まります。指定位置以降の既存ルールは 1 つずつずれます(上書きはされません)。たとえばindexで位置 n にルールを置くと、既存のインデックス n、n+1、n+2 などのルールは 1 つずつずれ、新しい位置は n+1、n+2、n+3 になります。インデックスが範囲外の場合、メソッドは HTTP ステータスコード400を返します。
定義を変えずにルールの順序だけを変えるには、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 に置き、ルールセットの 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>