Skip to content
Cloudflare Docs

Create a redirect rule using Terraform

The following example defines a single redirect rule for a zone using Terraform. The rule creates a static URL redirect for visitors requesting the contacts page using an old URL.

# Single Redirects resource
resource "cloudflare_ruleset" "single_redirects_example" {
zone_id = "<ZONE_ID>"
name = "redirects"
description = "Redirects ruleset"
kind = "zone"
phase = "http_request_dynamic_redirect"
rules {
ref = "redirect_old_url"
description = "Redirect visitors still using old URL"
expression = "(http.request.uri.path matches \"^/contact-us/\")"
action = "redirect"
action_parameters {
from_value {
status_code = 301
target_url {
value = "/contacts/"
}
preserve_query_string = false
}
}
}
}

Use the ref 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 Troubleshooting in the Terraform documentation.

Additional resources

For additional guidance on using Terraform with Cloudflare, refer to the following resources: