A rewrite policy allows you to modify the Uniform Resource Identifier (URI) of a request. You can configure a rewrite policy to modify the URI of a request that accesses your function by using a custom domain name. This topic describes the rewrite methods of custom domain names and how to configure a rewrite policy for your custom domain name in the Function Compute console.

Background information

You can use a rewrite policy in Function Compute to modify only the path and query parameters in a URI.

To modify the URI of a request that accesses your function by using a custom domain name, you can configure a rewrite policy for the custom domain name in the Function Compute console.

Rewrite methods

Function Compute allows you to configure rewrite policies for custom domain names at the route level and supports the following rewrite methods: exact rewrite, wildcard rewrite, and regex rewrite.

Exact rewrite

In the exact rewrite method, you can replace the value of the path parameter in the URI of your request with the string in the replacement rule only if the value of the path parameter exactly matches the string in the matching rule. For example, if the matching rule is /old and the replacement rule is /new, the URI /old is rewritten as /new in the request. The following table provides examples of matching by using the exact rewrite method.

Matching ruleReplacement ruleOriginal URIURI after rewrite
/old/new/old/new
/test/oldNot matched
/serviceName.Qualifier/serviceA/serviceName.Qualifier/serviceA

Wildcard rewrite

The wildcard rewrite method provides a simple matching method that can meet most of your business requirements.

In the wildcard rewrite method, you can use an asterisk (*) as a wildcard character to specify a string in any length in a matching rule. The replacement rule can reference the strings that are matched based on the location of the wildcard character in the matching rule. For example, if the matching rule is /api/* and the replacement rule is /$1, the URI /api/getFunctionName is rewritten as /getFunctionName in the request. The following table provides examples of matching by using the wildcard rewrite method.

Note In the wildcard rewrite method, all strings are matched and processed as normal strings except for strings that contain the asterisk (*) and the caret (^).
  • *: matches a string in any length.
  • ^: specifies that matching starts from the beginning of the string.
Matching ruleReplacement ruleOriginal URIURI after rewrite
/old/new/old/new
/test/old/new
^/old/new/old/new
/test/oldNot matched
/api/*/$1/api/getFunctionName/getFunctionName
/api/getFunctionName&Version=1/getFunctionName&Version=1
/css/*/public/static/css/$1/css/style.css/public/static/css/style.css
/ServiceName/*/FunctionName/*/Qualifier/*/$1.$3/$2/ServiceName/svc-a/FunctionName/func-a/Qualifier/1/svc-a.1/func-a
/f..c/helloworld/f..c/helloworld
/funcNot matched

Regex rewrite

In the regex rewrite method, matching rules and replacement rules use standard regular expressions. For information about the regular expression syntax, see RE2 Regular expression syntax. The following table describes examples of matching by using the regex write method.

Note The regex rewrite method is an advanced method and is more complex in syntax than the wildcard rewrite and exact rewrite methods. We recommend that you use the exact rewrite method except for special scenarios.
Matching ruleReplacement ruleOriginal URIURI after rewrite
^/old/[a-z]+//new/old/ab//new
/test/a-b/Not matched
^/api/.+?/(.*)/api/v2/$1/api/v1/test/api/v2/test
/api/v1-pre/test/api/v2/test

Matching order and priority

If you use all preceding methods for rewrite policies in a routing rule, a request is matched with the policies based on the following order: policies that use the exact rewrite method, policies that use the wildcard rewrite method, and policies that use the regex write method.

Function Compute allows you to configure multiple rewrite policies in a routing rule. Therefore, a request may be matched with multiple rewrite policies. If a request is matched with multiple rewrite policies that use the same rewrite method, Function Compute match the request with these policies based on the configuration order of the policies. The matching stops and the URI of the request is replaced until the request is first successfully matched with a policy.

For example, the following policies that use the wildcard rewrite method are configured.
var rule1 = &WildcardRules{
    Match:       "/api/v1/*",
    Replacement: "/api/v2/$1",
}

var rule2 = &WildcardRules{
    Match:       "/api/*/*",
    Replacement: "/api/v3/$2",
}
  • If your rewrite policy is [rule1, rule2], the URI /api/v1/getFunctionName is rewritten as /api/v2/getFunctionName in the request.
  • If your rewrite policy is [rule2, rule1], the URI /api/v1/getFunctionName is rewritten as /api/v3/getFunctionName in the request.

Prerequisites

A service and a function are created. For more information, see Create a service and Create a function.

Procedure

You can configure a rewrite policy when you create a custom domain name. You can also add a rewrite policy for an existing custom domain name.

Configure a rewrite policy when you create a custom domain name

  1. Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Custom Domains.
  2. In the top navigation bar, select a region. On the Custom Domains page, click Add Custom Domain Name.
  3. On the Add Custom Domain Name page, configure the Domain Name parameter and click Rewrite Policy to the right of the Route parameter.
  4. In the Configure Rewrite Policy panel, click + Add Rewrite Policy, select a policy from the drop-down list, and then click OK. edit-rewriterule

Add a rewrite policy for an existing custom domain name

  1. Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Custom Domains.
  2. On the Custom Domains page, find the desired custom domain name and click Modify in the Actions column.
  3. In the Configure Rewrite Policy panel, click + Add Rewrite Policy, select a policy from the drop-down list, and then click OK. edit-rewriterule