APIを介して作成
APIを介してバルクリダイレクトを作成するには、次の手順を実行する必要があります。
- APIを介してバルクリダイレクトリストを作成します。
- ステップ1で作成したリストにアイテム(URLリダイレクト)を追加します。
- ステップ1で作成したリストを有効にするために、APIを介してバルクリダイレクトルールを作成します。
リストを作成操作を使用して、新しいバルクリダイレクトリストを作成します。リストのkindはredirectでなければなりません。
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rules/lists \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "name": "my_redirect_list", "description": "私のリダイレクトリスト。", "kind": "redirect"}'レスポンスは次のようになります。
{ "result": { "id": "f848b6ccb07647749411f504d6f88794", "name": "my_redirect_list", "description": "私のリダイレクトリスト。", "kind": "redirect", "num_items": 0, "num_referencing_filters": 0, "created_on": "2021-10-28T09:11:42Z", "modified_on": "2021-10-28T09:11:42Z" }, "success": true, "errors": [], "messages": []}リストIDに注意してください — 次のステップで必要になります。
リスト操作の詳細については、リストAPIドキュメントを参照してください。
リストアイテムを作成操作を使用して、URLリダイレクトアイテムをリストに追加します。前のステップで取得したリストIDをエンドポイントURLに入力します。
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rules/lists/f848b6ccb07647749411f504d6f88794/items \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '[ { "redirect": { "source_url": "example.com/blog/", "target_url": "https://example.com/blog/latest" } }, { "redirect": { "source_url": "example.net/", "target_url": "https://example.net/under-construction.html", "status_code": 307 } }]'レスポンスは次のようになります。
{ "result": { "operation_id": "92558f8b296d4dbe9d0419e0e53f6622" }, "success": true, "errors": [], "messages": []}これは非同期操作です。レスポンスにはoperation_idが含まれており、バルク操作のステータスを取得操作を使用して操作が正常に完了したかどうかを確認します。
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rules/lists/bulk_operations/92558f8b296d4dbe9d0419e0e53f6622 \--header "Authorization: Bearer <API_TOKEN>"操作がすでに正常に完了している場合、レスポンスは次のようになります。
{ "result": { "id": "92558f8b296d4dbe9d0419e0e53f6622", "status": "completed", "completed": "2021-10-28T09:15:42Z" }, "success": true, "errors": [], "messages": []}バルクリダイレクトリストは基本的にURLリダイレクトのコンテナであるため、バルクリダイレクトルールを作成してリスト内のURLリダイレクトを有効にする必要があります。
バルクリダイレクトルールをアカウントレベルのhttp_request_redirectフェーズのエントリポイントルールセットに追加します。ルールセットの作成やルールのリストを提供する方法については、ルールセットAPIドキュメントを参照してください。
バルクリダイレクトルールには以下が必要です:
actionをredirectに設定- 追加の設定を含む
action_parametersオブジェクト — 詳細については、API JSONオブジェクト: バルクリダイレクトルールを参照してください。
以下のアカウントルールセットを作成操作のリクエストは、アカウントレベルのhttp_request_redirectフェーズのエントリポイントルールセットを作成し、単一のリダイレクトルールを定義します。この操作は、http_request_redirectフェーズのエントリポイントルールセットをまだ作成していない場合に使用します。
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/rulesets \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "name": "私のリダイレクトルールセット", "kind": "root", "phase": "http_request_redirect", "rules": [ { "expression": "http.request.full_uri in $my_redirect_list", "description": "バルクリダイレクトルール。", "action": "redirect", "action_parameters": { "from_list": { "name": "my_redirect_list", "key": "http.request.full_uri" } } } ]}'レスポンスは次のようになります。
{ "result": { "id": "528f4f03bf0da53a29907199625867be", "name": "私のリダイレクトルールセット", "kind": "root", "version": "1", "rules": [ { "id": "8da312df846b4258a05bcd454ea943be", "version": "1", "expression": "http.request.full_uri in $my_redirect_list", "description": "バルクリダイレクトルール。", "action": "redirect", "action_parameters": { "from_list": { "name": "my_redirect_list", "key": "http.request.full_uri" } }, "last_updated": "2021-10-28T09:20:42Z" } ], "last_updated": "2021-10-28T09:20:42Z", "phase": "http_request_redirect" }, "success": true, "errors": [], "messages": []}すでにhttp_request_redirectフェーズのエントリポイントルールセットがある場合は、次の例のようにアカウントルールセットを更新操作を使用します。
curl --request PUT \https://api.cloudflare.com/client/v4/accounts/{account_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "name": "私のリダイレクトルールセット", "kind": "root", "phase": "http_request_redirect", "rules": [ { "expression": "http.request.full_uri in $my_redirect_list_2", "description": "バルクリダイレクトルール1", "action": "redirect", "action_parameters": { "from_list": { "name": "my_redirect_list_1", "key": "http.request.full_uri" } } }, { "expression": "http.request.full_uri in $my_redirect_list_2", "description": "バルクリダイレクトルール2", "action": "redirect", "action_parameters": { "from_list": { "name": "my_redirect_list_2", "key": "http.request.full_uri" } } } ]}'レスポンスは次のようになります。
{ "result": { "id": "67013aa153df4e5fbda92f92bc979331", "name": "default", "description": "", "kind": "root", "version": "2", "rules": [ { "id": "8be62ab2ef9a4a41af30c24ff8e73e41", "version": "1", "action": "redirect", "action_parameters": { "from_list": { "name": "my_redirect_list_1", "key": "http.request.full_uri" } }, "expression": "http.request.full_uri in $my_redirect_list_1", "description": "バルクリダイレクトルール1", "last_updated": "2021-12-03T15:38:51.658387Z", "ref": "8be62ab2ef9a4a41af30c24ff8e73e41", "enabled": true }, { "id": "97e38797fb2b4b22a4919800f1318a5c", "version": "1", "action": "redirect", "action_parameters": { "from_list": { "name": "my_redirect_list_2", "key": "http.request.full_uri" } }, "expression": "http.request.full_uri in $my_redirect_list_2", "description": "バルクリダイレクトルール2", "last_updated": "2021-12-03T15:38:51.658387Z", "ref": "97e38797fb2b4b22a4919800f1318a5c", "enabled": true } ], "last_updated": "2021-12-03T15:38:51.658387Z", "phase": "http_request_redirect" }, "success": true, "errors": [], "messages": []}バルクリダイレクトオブジェクト(リスト、リストアイテム、ルール)を管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも以下の権限を持っている必要があります:
- アカウント > アカウントルールセット > 編集
- アカウント > アカウントフィルターリスト > 編集