All Products
Search
Document Center

Function Compute:Configure a rewrite policy (in public preview)

Last Updated:Jun 20, 2026

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

/old

/new

/old

/new

/test/old

Mismatch

/serviceName.Qualifier

/serviceA

/serviceName.Qualifier

/serviceA

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.

Note

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

/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 a regex rewrite, the matching rule and replacement rule follow standard regular expression syntax. For more information about the syntax, see regular expression syntax.

Note

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

^/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 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/getFunctionName is rewritten to/api/v2/getFunctionName.

  • If the rewrite policies are ordered as [rule2, rule1], a request for/api/v1/getFunctionName is rewritten to/api/v3/getFunctionName.

Prerequisites

Create a function

Procedure

You can add a rewrite policy to a new or an existing custom domain name.

New domain name

  1. Log on to theFunction Compute console. In the left-side navigation pane, chooseFunction Management > Custom Domain Name.

  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, enter a domain name. In the Routing Settings section, click Configure under Configuration.

  4. 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 /old and 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

  1. Log on to theFunction Compute console. In the left-side navigation pane, chooseFunction Management > Custom Domain Name.

  2. 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.

  3. On the Modify Custom Domain Name page, go to the Routing Settings section and click Configure under Configuration.

  4. In the Configure Rewrite Policy panel, click + Add Rewrite Policy, configure the policy, and then click OK.