Create a rule via API
Use the Rulesets API to create rewrite URL rules via API. Refer to the Rules examples gallery for common use cases.
When creating a rewrite URL 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.
Follow this workflow to create a rewrite URL rule for a given zone via API:
-
Use the List zone rulesets operation to check if there is already a ruleset for the
http_request_transform
phase at the zone level. -
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
- kind:
-
Use the Update a zone ruleset operation to add a rewrite URL 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: Add a rule that performs a static URL rewrite
The following example sets the rules of an existing phase ruleset ({ruleset_id}
) to a single rewrite URL rule — performing a static rewrite of the URI path — using the Update a zone ruleset operation:
The response contains the complete definition of the ruleset you updated.
Example: Add a rule that performs a dynamic URL rewrite
The following example sets the rules of an existing phase ruleset ({ruleset_id}
) to a single rewrite URL rule — performing a dynamic rewrite of the URI path — using the Update a zone ruleset operation:
The response contains the complete definition of the ruleset you updated.
The API token used in API requests to manage rewrite URL rules must have at least the following permissions:
- Account > Transform Rules > Edit
- Account > Account Rulesets > Read