Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

ルールセット内のルールを更新する

最終更新 Markdown で表示Agent セットアップ

アカウントまたはゾーン単位で、ルールセット内の既存ルールに 1 つ以上の変更を適用します。

次のいずれかの API エンドポイントを使います。

ルールの定義を更新してフィールドを変えるか、ルールセット内のルールの順序を変更できます。このメソッドを呼ぶと、ルールセットの新しいバージョンが作成されます。

ルールの定義を更新する

ルールの定義を更新するには、リクエスト本文に新しいルール定義を含めます。値を変えないフィールドでも、新しいルール定義に含めたいルールフィールドはすべて含める必要があります。

レスポンスには、ルール更新後の完全なルールセットが含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • Transform Rules Write
  • Select Configuration Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
Update an account ruleset rulebash
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 にルールを置くと、既存のインデックス nn+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>" }
	]
}

例 1

次の position オブジェクト付きリクエストは、ルール $RULE_ID_2 を先頭のルールにします。

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
Update a zone ruleset rulebash
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>

例 2

次の position オブジェクト付きリクエストは、ルール $RULE_ID_2 をルール 3 のあとに置きます。

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
Update a zone ruleset rulebash
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>

例 3

次の position オブジェクト付きリクエストは、ルール $RULE_ID_1 を位置 3 に置き、ルールセットの 3 番目のルールにします。

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
Update a zone ruleset rulebash
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>

役に立ちましたか?