All Products
Search
Document Center

Function Compute:Configure a rewrite policy (public review)

Last Updated:Feb 02, 2024

A rewrite policy allows you to modify the request Uniform Resource Identifier (URI). You can configure a rewrite policy to modify the request URI sent to a function. This topic describes rewrite patterns and how to configure a rewrite policy for a custom domain name in the Function Compute console.

Background

In Function Compute, rewrite policies support the modification of the path and query parameters in a request URI. However, the following parameters in a request URI cannot be modified: scheme, host, port, and fragment.

Function Compute allows you to configure a rewrite policy for a custom domain name to rewrite a request URI sent to a function.

Rewrite patterns

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

Exact rewrite

The exact rewrite pattern means that if the path parameter in a request is an exact match of a matching rule, the path string is replaced with the string in a replacement rule. For example, if a matching rule contains the string /old and a replacement rule contains the string /new, /old in a request is rewritten and replaced with /new. The following table describes matching examples based on specific replacement rules.

Matching rule

Replacement rule

Original URI

Rewritten URI

/old

/new

/old

/new

/test/old

Mismatch

/serviceName.Qualifier

/serviceA

/serviceName.Qualifier

/serviceA

Wildcard rewrite

The wildcard rewrite pattern provides a simple matching method that can be applied to most scenarios.

To use the wildcard pattern, you can use the asterisk wildcard character (*) to match strings. The matched strings can be referenced in a replacement rule based on the location of the wildcard character. For example, if a matching rule is /api/* and a replacement rule is /$1, the matched string /api/getFunctionName in a request is rewritten and replaced with /getFunctionName. The following table describes matching examples based on specific replacement rules.

Note

In the wildcard rewrite pattern, asterisks (*) and carets (^) have special meanings, and the rest of the strings are matched as common strings.

  • *: matches any string.

  • ^: matches a string from the beginning of the string.

Matching rule

Replacement rule

Original URI

Rewritten URI

/old

/new

/old

/new

/test/old

/new

^/old

/new

/old

/new

/test/old

Mismatch

/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

Mismatch

Regex rewrite

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

Note

The regex rewrite pattern is an advanced rewriting method that has complicated syntax rules. This pattern is suitable for special scenarios. In most cases, we recommend that you use the exact rewrite pattern.

Matching rule

Replacement rule

Original URI

Rewritten URI

^/old/[a-z]+/

/new

/old/ab/

/new

/test/a-b/

Mismatch

^/api/.+?/(.*)

/api/v2/$1

/api/v1/test

/api/v2/test

/api/v1-pre/test

/api/v2/test

Matching order and priority

The following priorities of the three rewrite patterns are arranged in descending order: exact rewrite, wildcard rewrite, and regex rewrite.

Function Compute allows you to configure multiple rewrite policies in a routing rule. A request may be matched to multiple rewrite policies. If a request is matched with multiple rewrite policies in a rewrite pattern, Function Compute matches the request with the policies based on the points in time when you created the policies. If the request is first matched with a policy, matching stops and the strings in the request are rewritten and replaced.

The following sample code provides examples of rewrite rules:

var rule1 = &WildcardRules{
    Match:       "/api/v1/*",
    Replacement: "/api/v2/$1",
}

var rule2 = &WildcardRules{
    Match:       "/api/*/*",
    Replacement: "/api/v3/$2",
}
  • If the rewrite policy [rule1, rule2] is configured for a user, the string /api/v1/getFunctionName in a request is rewritten and replaced with /api/v2/getFunctionName.

  • If the rewrite policy [rule2, rule1] is configured for the user, the string /api/v1/getFunctionName in a request is rewritten and replaced with /api/v3/getFunctionName.

Prerequisites

A function is created. For more information, see the "Create a function" section of the Manage functions topic.

Procedure

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

Configure a rewrite policy for a new 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 Domain Names page, click Add Custom Domain Name.

  3. On the Add Custom Domain Name page, specify a domain name for the Domain Name parameter. In the Routing Settings section, click Configure below the Rewrite Policy parameter.

  4. In the Configure Rewrite Policy panel, click + Add Rewrite Policy, select a policy pattern based on your business requirements, configure the parameters for the policy, and then click OK.

    edit-rewriterule

Configure 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. In the top navigation bar, select a region. In the domain name list, find the domain name that you want to manage and click Modify in the Actions column.

  3. On the Modify Custom Domain Name page, find the Routing Settings section and click Configure below the Rewrite Policy parameter.

  4. In the Configure Rewrite Policy panel, click + Add Rewrite Policy, select a policy pattern based on your business requirements, configure the parameters for the policy, and then click OK.

    edit-rewriterule