All Products
Search
Document Center

Edge Security Acceleration:Configure URL rewrite rules with Terraform

Last Updated:Mar 27, 2026

You can use Terraform to configure URL rewrite rules for a site that transform the path and query string based on expressions.

Install Terraform and configure permissions

Install and configure Terraform on your computer

For more information about how to use Terraform on your computer, see Install and configure Terraform.

  • Create an AccessKey pair for a RAM user. An Alibaba Cloud account has all permissions on resources. If the AccessKey pair of your Alibaba Cloud account is leaked, your resources are exposed to great risks. We recommend that you use the AccessKey pair of a RAM user. For more information, see Create an AccessKey pair.

  • Create environment variables to store identity credentials.

    You can create and view your AccessKey on the AccessKey Management page.

    If the environment variables are not configured, identity verification fails when you run the Terraform template.

Use an online service (no installation or permission configuration required)

If you do not want to install Terraform, you can use the online service Cloud Shell.

Alibaba Cloud Cloud Shell is a free operations and maintenance (O&M) product. It is pre-installed with Terraform components and configured with identity credentials. Therefore, you can run Terraform commands directly in Cloud Shell. For more information, see Create resources with Terraform.

Important

When you use Terraform in Cloud Shell, its destroy feature can cause data loss. We recommend that you use Cloud Shell only for simple and quick operations, such as debugging. For more information about the limits, see Limits.

Resources

Configure a URL rewrite rule

  1. Create a configuration file named main.tf and copy the following code to the file. This code creates a URL rewrite rule for a site.

    # 1. Query information about an existing site.
    data "alicloud_esa_sites" "default" {
      site_name = "{{DOMAIN}}"  # Replace with the domain name of your site that has been added to Edge Security Acceleration, such as example.com.
    }
    
    # 2. Configure the URL rewrite: Rewrite paths that start with /images/ to /static/img/ and remove all query parameters.
    resource "alicloud_esa_rewrite_url_rule" "example" {
      site_id                   = data.alicloud_esa_sites.default.sites[0].site_id
      rule_name                 = "rewrite-images-prefix"
      rule_enable               = "on"
      rule                      = "starts_with(http.request.uri.path, \"/images/\")"
      rewrite_uri_type          = "dynamic"
      uri                       = "regex_replace(http.request.uri.path, \"^/images/\", \"/static/img/\")"
      rewrite_query_string_type = "static"
      query_string              = ""  # An empty string indicates that all query parameters are removed.
    }
    
  2. Navigate to the directory that contains the configuration files. Then, run the following command to initialize the Terraform environment.

    terraform init

    image

  3. Run the following command to validate the syntax and configuration of the Terraform files.

    terraform validate

    If the output is similar to the following figure, the validation is successful.

    image

  4. Run the following command to preview the changes that will be applied.

    terraform plan
  5. Run the following command to execute the Terraform script.

    terraform apply
  6. When prompted, enter yes to confirm the operation.

Verify the result

Run terraform show

Run the following command in your working directory to view details about the resources that Terraform created:

terraform show

Verify in the console

  1. In the ESA console, select Site Management, and in the Website column, click the target site.

  2. In the navigation pane on the left, choose Rules > Transform Rules to view the created rule and its configuration.

(Optional) Clean up resources

If you no longer need the resources created or managed by Terraform, you can run the terraform destroy command to release the resources.

terraform destroy

References

The rewrite_uri_type parameter

The following table describes the valid values for the rewrite_uri_type parameter of the Terraform alicloud_esa_rewrite_url_rule resource.

Value

Description

Description

static

Static

Rewrites the path to a fixed destination.

dynamic

Dynamic

Uses an expression to dynamically calculate the destination path.

The rewrite_query_string_type parameter

In the same resource, the following table describes the valid values for the rewrite_query_string_type parameter.

Value

Description

Description

static

Static

Replaces the query string with the fixed value specified by query_string. Setting this to an empty string removes all query parameters.

dynamic

Dynamic

Uses an expression to dynamically generate the query string.