All Products
Search
Document Center

API Gateway:Configure an HTTP rewrite policy

Last Updated:Jun 15, 2026

An HTTP rewrite policy modifies a request's path and hostname before forwarding to a backend service, giving you precise control over routing to meet your business and architectural requirements.

Configure an HTTP rewrite policy

HTTP rewriting modifies the path and hostname of the original request.

  1. Cloud-native API Gateway provides two ways to configure an HTTP rewrite policy: for APIs managed outside an instance and for APIs managed within an instance.

    APIs outside an instance

    1. Log on to the Cloud-native API Gateway console. In the navigation pane on the left, click API, and then select a region from the top menu bar.

    2. Click the target API. From the All instances drop-down list at the top, select the target instance, or select All Instances.

    3. On the Routes tab, select the target route.

    APIs within an instance

    1. Log on to the Cloud-native API Gateway console. In the navigation pane on the left, click Instance, and then select a region from the top menu bar.

    2. On the Instance page, click the ID of the target gateway instance. In the navigation pane on the left, click API, and then click the target API.

    3. On the Routes tab, select the target route.

  2. Click the Configure Policy tab. Then, in the Inbound Processing section, click Enable Policy/Plug-in.

  3. Click the HTTP Rewrite card. In the Add Policy: HTTP Rewrite panel, configure the parameters and click Add.

Path rewrite

Cloud-native API Gateway supports two path rewrite methods: prefix rewrite and regex rewrite.

Prefix rewrite

A prefix rewrite changes the path prefix of a request.

Example 1

To change a request path from /app/test to /test before forwarding it to a backend service, use the following configuration:

  • Route matching condition: The matching type is Prefix Match, and the path is /app/.

  • Rewrite: The rewrite type is Prefix Rewrite, and the path is /.

Note

The path for the route matching condition must be /app/. This is because a prefix rewrite only replaces the string matched by the prefix rule. If the path for the condition were set to /app, the rewritten path would become //test, which is not the intended result.

Example 2

To change a request path from /v1/test to /v2/test, use the following configuration:

  • Route matching condition: The matching type is Prefix Match, and the path is /v1.

  • Rewrite: The rewrite type is Prefix Rewrite, and the path is /v2.

Important

A prefix rewrite requires the route's matching type to be Prefix Match. This rewrite type is not supported for routes that use Exact Match or Regex Match. Because a prefix match applies to all requests that share a path prefix, carefully consider if all matching requests should be rewritten. If not, consider using a regex rewrite.

Regex rewrite

A regex rewrite modifies a portion of the request path by using a mode parameter (the regex pattern to match) and a replacement parameter (the substitution string). For syntax details, see regular expression syntax.

Example 1

To change a request path from /aaa/one/bbb/one/ccc to /aaa/two/bbb/two/ccc, use the following configuration:

  • Route matching condition: The matching type is Exact Match, and the path is /aaa/one/bbb/one/ccc.

  • Rewrite: The rewrite type is Regex Rewrite, the mode is one, and the replacement is two.

Example 2

To remove the /httpbin prefix from a path like /httpbin/(.*)/(.*) and swap the two captured groups, use the following configuration:

  • Route matching condition: The matching type is Regex Match, and the path is /httpbin/(.*)/(.*).

  • Rewrite: The rewrite type is Regex Rewrite, the mode is /httpbin/(.*)/(.*), and the replacement is /\2/\1. In this string, \1 represents the first captured group from the regular expression, and \2 represents the second. This is similar to the $1 and $2 variables used in Nginx.

Note

Regex rewrite is an advanced feature with complex syntax that is typically used for special cases. We recommend using a prefix rewrite if it meets your requirements.

Hostname rewrite

Cloud-native API Gateway supports exact hostname rewriting.

For example, to change the request hostname from test.com to dev.com, set the rewrite hostname to dev.com in the policy configuration.