Create a redirect rule using Terraform (2)
Last updated Aug 25, 2026 Note
Terraform code snippets below refer to the v4 SDK only.
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:
- Terraform documentation
- Cloudflare Provider for Terraform ↗︎ (reference documentation)
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/url-forwarding/single-redirects/terraform-example/#page","headline":"Create a redirect rule using Terraform","description":"Create Single Redirect rules using the Terraform Cloudflare provider.","url":"https://developers.cloudflare.com/rules/url-forwarding/single-redirects/terraform-example/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/url-forwarding/single-redirects/terraform-example/og.png?v=28d23a7763d0f86b","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":["Terraform","Redirects"]}
description: Create Snippets using the Terraform Cloudflare provider. title: Configure Snippets using Terraform image: https://developers.cloudflare.com/rules/snippets/create-terraform/og.png?v=fd1da57a60a8b725
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.
Docs