By configuring HTTP rewrite policies, you can modify the path and hostname of a request before forwarding it to the backend service. This allows you to meet specific business requirements and architectural needs. Rewrite policies enable you to precisely control the path and hostname of a request and ensure that it is correctly routed to the appropriate service or endpoint.
Configure HTTP rewrite policies
HTTP rewrite policies are primarily used to modify the path and hostname in the original request.
Cloud-native API Gateway provides two methods to configure HTTP rewrite policies: outside an instance and inside an instance:
API outside an instance
Log on to the Cloud-native API Gateway console. In the left-side navigation pane, select API, and select a region in the top menu bar.
Click the target API. You can select the instance for which you want to configure HTTP rewrite policies from the drop-down list, or select All Instances.

Find the target route from the Routes tab.
API inside an instance
Log on to the Cloud-native API Gateway console. In the left-side navigation pane, select Instances, and select a region in the top menu bar.
On the Instance page, click the ID of the target gateway instance. In the left-side navigation pane, select API, and click the target API.
Find the target route from the Routes page.
Click the Policy Configuration tab, and then click Enable Policy/Plug-in in the Inbound Processing section.
Click the HTTP Rewrite card, configure the parameters in the Add Policy: HTTP Rewrite panel, and then click Add.
Path rewrite
For path rewrites, cloud-native API gateways support two rewrite modes: prefix rewrite and regex rewrite.
Prefix rewrite
Changes the path prefix in a request.
Example 1
The path in the original request is /app/test, but the path forwarded to the backend service is /test. We recommend that you configure the following policy:
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
/.
The path in the route matching condition must be set to /app/ because prefix rewrite only modifies the prefix matching string. If the path in the route matching condition is set to /app, the rewritten path will be //test, which is not the expected result.
Example 2
The path in the original request is /v1/test, but the path forwarded to the backend service is /v2/test. We recommend that you configure the following policy:
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.
If you want the rewrite type of the rewrite policy to be Prefix Rewrite, the matching type of the route must be Prefix Match. The matching type of the route cannot be Exact Match or RegEx Match. If the matching type of a route is set to Prefix Match, all requests with paths that contain the specified prefix are matched and rewritten. Before you set the rewrite type of the rewrite policy to Prefix Rewrite, you must confirm that all requests need to be rewritten. Otherwise, we recommend that you set the rewrite type to Exact Rewrite.
Regex rewrite
Partially changes the path in a request. A regex rewrite configuration consists of the mode and replacement fields. The value of the mode field is used to match the specified parts that you want to modify in the original path. The value of the replacement field is used to replace the matched parts. For information about the syntax of regular expressions, see Regular expression syntax.
Example 1
The path in the original request is /aaa/one/bbb/one/ccc, but the path forwarded to the backend service is /aaa/two/bbb/two/ccc. We recommend that you configure the following policy:
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 istwo.
Example 2
The path in the original request is /httpbin/(.*)/(.*). You want to remove the /httpbin prefix and swap the positions of the two regular expression parts. We recommend that you configure the following policy:
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.\1represents the first string matched by the regular expression, and\2represents the second string matched by the regular expression. This usage corresponds to $1 and $2 in Nginx.
The regex rewrite method is more complex than the prefix rewrite and exact rewrite methods. We recommend that you use the exact rewrite method instead of the regex rewrite method if possible.
Hostname rewrite
Cloud-native API gateways allow you to modify hostnames in requests by using the exact rewrite method.
For example, the hostname in the original request is test.com, but the hostname forwarded to the backend service is dev.com. In the rewrite policy, set the rewrite hostname to dev.com.