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 resourceresource "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.
For additional guidance on using Terraform with Cloudflare, refer to the following resources:
- Terraform documentation
- Cloudflare Provider for Terraform ↗ (reference documentation)
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-