You can configure a rewrite policy to modify the request URI of a request that reaches your function through a custom domain name. This topic describes the available rewrite patterns and shows you how to configure a rewrite policy for your custom domain name in the Function Compute console.
Background
In Function Compute, a rewrite policy can modify only thepath andquery components of a request URI. It does not support modifying thescheme,host,port, orfragment.
Rewrite patterns
Function Compute supports route-level rewrites for custom domain names and provides three rewrite patterns: exact rewrite, wildcard rewrite, and regex rewrite.
Exact rewrite
In an exact rewrite, if the request path exactly matches the matching rule, the path is replaced with the replacement rule. For example, if the matching rule is/old and the replacement rule is/new, a request for/old is rewritten to/new.
|
Matching rule |
Replacement rule |
Original URI |
Rewritten URI |
|
|
|
|
|
|
|
Mismatch |
||
|
|
|
|
|
Wildcard rewrite
Wildcard rewrite provides a simple way to match requests and is suitable for most use cases.
In the replacement rule, you can reference the string captured by each asterisk according to its position. For example, if the matching rule is/api/* and the replacement rule is/$1, a request for/api/getFunctionName is rewritten to/getFunctionName.
In a wildcard rewrite,* and^ have special meanings. All other characters are matched as literal strings.
-
*matches any string. -
^anchors the match to the beginning of the string.
|
Matching rule |
Replacement rule |
Original URI |
Rewritten URI |
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
Mismatch |
||
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mismatch |
Regex rewrite
In a regex rewrite, the matching rule and replacement rule follow standard regular expression syntax. For more information about the syntax, see regular expression syntax.
Regex rewrite is an advanced feature with complex syntax and is intended for special use cases. For most scenarios, we recommend that you use wildcard rewrite.
|
Matching rule |
Replacement rule |
Original URI |
Rewritten URI |
|
|
|
|
|
|
|
Mismatch |
||
|
|
|
|
|
|
|
|
Matching order and priority
The priority of the three rewrite patterns, from highest to lowest, is:exact rewrite → wildcard rewrite → regex rewrite.
You can configure multiple rewrite policies for a single routing rule. If a request matches multiple policies of the same type, Function Compute applies the first one in the specified order and ignores the rest.
For example, consider the following wildcard rewrite policies:
var rule1 = &WildcardRules{
Match: "/api/v1/*",
Replacement: "/api/v2/$1",
}
var rule2 = &WildcardRules{
Match: "/api/*/*",
Replacement: "/api/v3/$2",
}
-
If the rewrite policies are ordered as [rule1, rule2], a request for
/api/v1/getFunctionNameis rewritten to/api/v2/getFunctionName. -
If the rewrite policies are ordered as [rule2, rule1], a request for
/api/v1/getFunctionNameis rewritten to/api/v3/getFunctionName.
Prerequisites
Procedure
You can add a rewrite policy to a new or an existing custom domain name.
New domain name
-
Log on to theFunction Compute console. In the left-side navigation pane, choose.
-
In the top navigation bar, select a region. On the Custom Domains page, click Add Custom Domain Name.
-
On the Add Custom Domain Name page, enter a domain name. In the Routing Settings section, click Configure under Configuration.
-
In the Configure Rewrite Policy panel, click + Add Rewrite Policy, configure the policy, and then click OK.
Rewrite policies support three Policy Type options: exact rewrite (for example, matching rule
/oldand replacement rule/new), wildcard rewrite (for example, matching rule/api/*and replacement rule$1), and regex rewrite (for example, matching rule^/api/.+?/(.*)and replacement rule/api/v2/$1). For each policy, you must configure a Matching rule and a Replacement rule.
Existing domain name
-
Log on to theFunction Compute console. In the left-side navigation pane, choose.
-
In the top navigation bar, select a region. In the domain name list, find the target domain name and click Modify in the Actions column.
-
On the Modify Custom Domain Name page, go to the Routing Settings section and click Configure under Configuration.
-
In the Configure Rewrite Policy panel, click + Add Rewrite Policy, configure the policy, and then click OK.