コンテンツにスキップ

APIを介してルールを作成する

Rulesets APIを使用して、APIを介してリダイレクトルールを作成します。

ゾーンレベルでhttp_request_dynamic_redirectフェーズのエントリポイントルールセットにリダイレクトルールを追加します。Rulesets APIのドキュメントを参照して、ルールセットの作成やルールセットにルールのリストを提供する方法についての詳細を確認してください。

基本的なルール設定

リダイレクトルールには以下が必要です:

  • actionredirectに設定
  • 追加の設定を含むaction_parametersオブジェクト — 詳細については利用可能な設定を参照してください。

例リクエスト

以下のリクエストは、ゾーンルールセットの作成操作を使用して、ゾーンレベルでhttp_request_dynamic_redirectフェーズのエントリポイントルールセットを作成し、動的URLリダイレクトを持つシングルリダイレクトルールを定義します。まだhttp_request_dynamic_redirectフェーズのエントリポイントルールセットを作成していない場合は、この操作を使用してください。

リクエスト
curl https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"name": "リダイレクトルールルールセット",
"kind": "zone",
"phase": "http_request_dynamic_redirect",
"rules": [
{
"expression": "(ip.geoip.country eq \"GB\" or ip.geoip.country eq \"FR\") and http.request.uri.path eq \"/\"",
"description": "GBおよびFRのユーザーをホームページからローカライズされたサイトにリダイレクトします。",
"action": "redirect",
"action_parameters": {
"from_value": {
"target_url": {
"expression": "lower(concat(\"https://\", ip.geoip.country, \".example.com\"))"
},
"status_code": 307,
"preserve_query_string": true
}
}
}
]
}'

レスポンス

{
"result": {
"id": "528f4f03bf0da53a29907199625867be",
"name": "リダイレクトルールルールセット",
"kind": "zone",
"version": "1",
"rules": [
{
"id": "235e557b92fd4e5e8753ee665a9ddd75",
"version": "1",
"expression": "(ip.geoip.country eq \"GB\" or ip.geoip.country eq \"FR\") and http.request.uri.path eq \"/\"",
"description": "GBおよびFRのユーザーをホームページからローカライズされたサイトにリダイレクトします。",
"action": "redirect",
"action_parameters": {
"from_value": {
"target_url": {
"expression": "lower(concat(\"https://\", ip.geoip.country, \".example.com\"))"
},
"status_code": 307,
"preserve_query_string": true
}
},
"last_updated": "2022-09-28T09:20:42Z"
}
],
"last_updated": "2022-09-28T09:20:42Z",
"phase": "http_request_dynamic_redirect"
},
"success": true,
"errors": [],
"messages": []
}

すでにhttp_request_dynamic_redirectフェーズのエントリポイントルールセットがある場合は、ゾーンルールセットの更新操作を代わりに使用してください。以下の例のように:

リクエスト
curl --request PUT \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"name": "リダイレクトルールルールセット",
"kind": "zone",
"phase": "http_request_dynamic_redirect",
"rules": [
{
"expression": "(ip.geoip.country eq \"GB\" or ip.geoip.country eq \"FR\") and http.request.uri.path eq \"/\"",
"description": "GBおよびFRのユーザーをホームページからローカライズされたサイトにリダイレクトします。",
"action": "redirect",
"action_parameters": {
"from_value": {
"target_url": {
"expression": "lower(concat(\"https://\", ip.geoip.country, \".example.com\"))"
},
"status_code": 307,
"preserve_query_string": true
}
}
},
{
"expression": "http.request.uri.path eq \"/contacts.html\"",
"description": "新しい連絡先ページにリダイレクトします。",
"action": "redirect",
"action_parameters": {
"from_value": {
"target_url": {
"value": "https://example.com/contact-us/"
},
"status_code": 308
}
}
}
]
}'

レスポンス

{
"result": {
"id": "528f4f03bf0da53a29907199625867be",
"name": "リダイレクトルールルールセット",
"description": "",
"kind": "zone",
"version": "2",
"rules": [
{
"id": "235e557b92fd4e5e8753ee665a9ddd75",
"version": "1",
"action": "redirect",
"action_parameters": {
"from_value": {
"target_url": {
"expression": "lower(concat(\"https://\", ip.geoip.country, \".example.com\"))"
},
"status_code": 307,
"preserve_query_string": true
}
},
"expression": "(ip.geoip.country eq \"GB\" or ip.geoip.country eq \"FR\") and http.request.uri.path eq \"/\"",
"description": "GBおよびFRのユーザーをホームページからローカライズされたサイトにリダイレクトします。",
"last_updated": "2022-10-03T15:38:51.658387Z",
"ref": "235e557b92fd4e5e8753ee665a9ddd75",
"enabled": true
},
{
"id": "cfad5efbfcd1440fb5b30cf30f95ece3",
"version": "1",
"action": "redirect",
"action_parameters": {
"from_value": {
"target_url": {
"value": "https://example.com/contact-us/"
},
"status_code": 308
}
},
"expression": "http.request.uri.path eq \"/contacts.html\"",
"description": "新しい連絡先ページにリダイレクトします。",
"last_updated": "2022-10-03T15:38:51.658387Z",
"ref": "cfad5efbfcd1440fb5b30cf30f95ece3",
"enabled": true
}
],
"last_updated": "2022-10-03T15:38:51.658387Z",
"phase": "http_request_dynamic_redirect"
},
"success": true,
"errors": [],
"messages": []
}

必要なAPIトークンの権限

リダイレクトルールを管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも以下の権限を持っている必要があります:

  • Zone > Single Redirect > Edit