Create a URL rewrite rule via API

Last updated Aug 25, 2026 Use the Rulesets API to create URL Rewrite Rules via API. Refer to the Rules examples gallery for common use cases.

If you are using Terraform, refer to Transform Rules configuration using Terraform.

Basic rule settings

When creating a URL rewrite rule via API, make sure you:

  • Set the rule action to rewrite.
  • Define the URL rewrite parameters in the action_parameters field according to the type of URL rewrite (static or dynamic).
  • Deploy the rule to the http_request_transform phase at the zone level.

Procedure

Follow this workflow to create a URL rewrite rule 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:
  3. kind: zone
  4. phase: http_request_transform
  5. Use the Update a zone ruleset operation to add a URL rewrite rule to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.

Make sure your API token has the required permissions to perform the API operations.

Example requests

<details>

<summary>

Example: Add a rule that performs a static URL rewrite

</summary>

The following example sets the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single URL rewrite rule — performing a static rewrite of the URI path — using the <a href="https://developers.cloudflare.com/api/resources/rulesets/methods/update/">Update a zone ruleset</a> operation. The response will contain the complete definition of the ruleset you updated.

<details>

<summary>

Required API token permissions

</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:

  • <code>Response Compression Write</code>
  • <code>Config Settings Write</code>
  • <code>Dynamic URL Redirects Write</code>
  • <code>Cache Settings Write</code>
  • <code>Custom Errors Write</code>
  • <code>Origin Write</code>
  • <code>Managed headers Write</code>
  • <code>Zone Transform Rules Write</code>
  • <code>Mass URL Redirects Write</code>
  • <code>Magic Firewall Write</code>
  • <code>L4 DDoS Managed Ruleset Write</code>
  • <code>HTTP DDoS Managed Ruleset Write</code>
  • <code>Sanitize Write</code>
  • <code>Transform Rules Write</code>
  • <code>Select Configuration Write</code>
  • <code>Bot Management Write</code>
  • <code>Zone WAF Write</code>
  • <code>Account WAF Write</code>
  • <code>Account Rulesets Write</code>
  • <code>Logs Write</code>
  • <code>Logs Write</code>

</details>

Update a zone rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "ref": "rewrite_eu_to_emea",
                        "expression": "(http.request.uri.query contains \"eu\")",
                        "description": "My first static URL rewrite rule",
                        "action": "rewrite",
                        "action_parameters": {
                                "uri": {
                                        "path": {
                                                "value": "/emea.html"
                                        }
                                }
                        }
                }
        ]
    }'
{
    "result": {
        "ref": "rewrite_eu_to_emea",
        "id": "<RULESET_ID>",
        "name": "Zone-level Transform Ruleset",
        "description": "Zone-level ruleset that will execute Transform Rules.",
        "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": "My first static URL rewrite rule",
                "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": []
}

Use the <code>ref</code> field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.

</details>

<details>

<summary>

Example: Add a rule that performs a dynamic URL rewrite

</summary>

The following example sets the rules of an existing phase ruleset (<code>$RULESET_ID</code>) to a single URL rewrite rule — performing a dynamic rewrite of the URI path — using the <a href="https://developers.cloudflare.com/api/resources/rulesets/methods/update/">Update a zone ruleset</a> operation. The response will contain the complete definition of the ruleset you updated.

<details>

<summary>

Required API token permissions

</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:

  • <code>Response Compression Write</code>
  • <code>Config Settings Write</code>
  • <code>Dynamic URL Redirects Write</code>
  • <code>Cache Settings Write</code>
  • <code>Custom Errors Write</code>
  • <code>Origin Write</code>
  • <code>Managed headers Write</code>
  • <code>Zone Transform Rules Write</code>
  • <code>Mass URL Redirects Write</code>
  • <code>Magic Firewall Write</code>
  • <code>L4 DDoS Managed Ruleset Write</code>
  • <code>HTTP DDoS Managed Ruleset Write</code>
  • <code>Sanitize Write</code>
  • <code>Transform Rules Write</code>
  • <code>Select Configuration Write</code>
  • <code>Bot Management Write</code>
  • <code>Zone WAF Write</code>
  • <code>Account WAF Write</code>
  • <code>Account Rulesets Write</code>
  • <code>Logs Write</code>
  • <code>Logs Write</code>

</details>

Update a zone rulesetbash

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
    --request PUT \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "rules": [
                {
                        "ref": "rewrite_2012_to_archive",
                        "expression": "starts_with(http.request.uri.path, \"/news/2012/\")",
                        "description": "My first dynamic URL rewrite rule",
                        "action": "rewrite",
                        "action_parameters": {
                                "uri": {
                                        "path": {
                                                "expression": "concat(\"/archive\", http.request.uri.path)"
                                        }
                                }
                        }
                }
        ]
    }'
{
    "result": {
        "id": "<RULESET_ID>",
        "name": "Zone-level Transform Ruleset",
        "description": "Zone-level ruleset that will execute Transform Rules.",
        "kind": "zone",
        "version": "2",
        "rules": [
            {
                "ref": "rewrite_2012_to_archive",
                "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": "My first dynamic URL rewrite rule",
                "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": []
}

Use the <code>ref</code> field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.

</details>


Required API token permissions

The API token used in API requests to manage URL Rewrite Rules must have at least the following permissions:

  • Account > Transform Rules > Edit
  • Account > Account Rulesets > Read

Was this helpful?

YesNo

On this page

Create a URL rewrite rule via API - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/transform/url-rewrite/create-api/#page","headline":"Create a URL rewrite rule via API","description":"Create URL rewrite rules using the Rulesets API.","url":"https://developers.cloudflare.com/rules/transform/url-rewrite/create-api/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/transform/url-rewrite/create-api/og.png?v=56c222bcb0c2e2fd","dateModified":"2026-08-25","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["URL rewrite"]}


description: Create URL rewrite rules in the Cloudflare dashboard. title: Create a URL rewrite rule in the dashboard image: https://developers.cloudflare.com/rules/transform/url-rewrite/create-dashboard/og.png?v=6e8470437d4a0a76


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt
Use this file to discover all available pages before exploring further.