跳转到内容
搜索文档

使用 Terraform 创建重定向规则

最后更新 查看 MarkdownAgent 设置

使用 Terraform Cloudflare 提供程序配置单次重定向 (Single Redirects) 规则。

以下示例使用 Terraform 为区域定义了一个单次重定向规则。该规则为使用旧 URL 访问联系人页面的访问者创建了静态 URL 重定向。

# 单次重定向资源
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
      }
    }
  }
}

使用 Terraform 时,通过 ref 字段可在更新后保持稳定的规则 ID。添加该字段可防止 Terraform 在变更时重建规则。更多信息请参阅 故障排除(Terraform 文档)。

其他资源

有关将 Terraform 与 Cloudflare 配合使用的其他指南,请参阅以下资源:

这篇文档对您有帮助吗?