コンテンツにスキップ

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

Rulesets APIを使用して、APIを介してURL書き換えルールを作成します。一般的なユースケースについては、ルールの例ギャラリーを参照してください。

基本的なルール設定

APIを介してURL書き換えルールを作成する際は、次のことを確認してください。

  • ルールアクションをrewriteに設定します。
  • URL書き換えのタイプ(静的または動的)に応じて、action_parametersフィールドにURL書き換えパラメータを定義します。
  • ルールをゾーンレベルのhttp_request_transformフェーズにデプロイします。

手順

Follow this workflow to create URL書き換えルール for a given zone via API:

  1. Use the List zone rulesets operation to check if there is already a ruleset for the http_request_transform phase at the zone level.

  2. If the phase ruleset does not exist, create it using the Create a zone ruleset operation. In the new ruleset properties, set the following values:

    • kind: zone
    • phase: http_request_transform
  3. Use the Update a zone ruleset operation to add URL書き換えルール to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.

API操作を実行するために、APIトークンに必要な権限があることを確認してください。

例リクエスト

例: 静的URL書き換えを行うルールを追加する

次の例では、既存のフェーズルールセット({ruleset_id})のルールを単一のURL書き換えルールに設定します。これは、ゾーンルールセットの更新操作を使用して、URIパスの静的書き換えを行います。

リクエスト
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 '{
"rules": [
{
"expression": "(http.request.uri.query contains \"eu\")",
"description": "私の最初の静的URL書き換えルール",
"action": "rewrite",
"action_parameters": {
"uri": {
"path": {
"value": "/emea.html"
}
}
}
}
]
}'

レスポンスには、更新したルールセットの完全な定義が含まれています。

レスポンス
{
"result": {
"id": "<RULESET_ID>",
"name": "ゾーンレベルの変換ルールセット",
"description": "変換ルールを実行するゾーンレベルのルールセット。",
"kind": "zone",
"version": "2",
"rules": [
{
"id": "<RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"uri": {
"path": {
"value": "/emea.html"
}
}
},
"expression": "(http.request.uri.query contains \"eu\")",
"description": "私の最初の静的URL書き換えルール",
"last_updated": "2021-04-14T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2021-04-14T14:42:04.219025Z",
"phase": "http_request_transform"
},
"success": true,
"errors": [],
"messages": []
}

例: 動的URL書き換えを行うルールを追加する

次の例では、既存のフェーズルールセット({ruleset_id})のルールを単一のURL書き換えルールに設定します。これは、ゾーンルールセットの更新操作を使用して、URIパスの動的書き換えを行います。

リクエスト
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 '{
"rules": [
{
"expression": "starts_with(http.request.uri.path, \"/news/2012/\")",
"description": "私の最初の動的URL書き換えルール",
"action": "rewrite",
"action_parameters": {
"uri": {
"path": {
"expression": "concat(\"/archive\", http.request.uri.path)"
}
}
}
}
]
}'

レスポンスには、更新したルールセットの完全な定義が含まれています。

レスポンス
{
"result": {
"id": "<RULESET_ID>",
"name": "ゾーンレベルの変換ルールセット",
"description": "変換ルールを実行するゾーンレベルのルールセット。",
"kind": "zone",
"version": "2",
"rules": [
{
"id": "<RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"uri": {
"path": {
"expression": "concat(\"/archive\", http.request.uri.path)"
}
}
},
"expression": "starts_with(http.request.uri.path, \"/news/2012/\")",
"description": "私の最初の動的URL書き換えルール",
"last_updated": "2021-04-14T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2021-04-14T14:42:04.219025Z",
"phase": "http_request_transform"
},
"success": true,
"errors": [],
"messages": []
}

必要なAPIトークンの権限

URL書き換えルールを管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも次の権限を持っている必要があります。

  • アカウント > 変換ルール > 編集
  • アカウント > アカウントルールセット > 読み取り