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 rule | Replacement rule | Original URI | URI after rewrite |
---|---|---|---|
/old | /new | /old | /new |
/test/old | Not 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.
*
) and the caret (^
). *
: matches a string in any length.^
: specifies that matching starts from the beginning of the string.
Matching rule | Replacement rule | Original URI | URI after rewrite |
---|---|---|---|
/old | /new | /old | /new |
/test/old | /new | ||
^/old | /new | /old | /new |
/test/old | Not 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 |
/func | Not 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.
Matching rule | Replacement rule | Original URI | URI 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.
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
- Log on to the Function 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, configure the Domain Name parameter and click Rewrite Policy to the right of the Route parameter.
- In the Configure Rewrite Policy panel, click + Add Rewrite Policy, select a policy from the drop-down list, and then click OK.
Add a rewrite policy for an existing custom domain name
- Log on to the Function Compute console. In the left-side navigation pane, choose .
- On the Custom Domains page, find the desired custom domain name and click Modify in the Actions column.
- In the Configure Rewrite Policy panel, click + Add Rewrite Policy, select a policy from the drop-down list, and then click OK.