Configure rewrite policies (Public Beta)

更新时间:
复制 MD 格式

A rewrite policy modifies the request URI for a custom domain name. This topic describes the available rewrite methods and explains how to configure a rewrite policy in the Function Compute console.

Background

A rewrite policy in Function Compute modifies only thepath andquery components of a URI. Modifying thescheme,host,port, orfragment components is not supported.

Rewrite methods

Custom domain names support route-level rewriting. Function Compute supports three rewrite methods: exact rewrite, wildcard rewrite, and regex rewrite.

Exact rewrite

An exact rewrite replaces the requestpath with the string from the replacement rule only if thepath exactly matches the matching rule. For example, if the matching rule is/old and the replacement rule is/new, a request to/old is rewritten to/new. The following table provides more examples.

Matching rule Replacement rule Original URI Rewritten URI
/old /new /old /new
/test/old No match
/serviceName.Qualifier /serviceA /serviceName.Qualifier /serviceA

Wildcard rewrite

A wildcard rewrite provides a simple method for flexible matching.

A wildcard rewrite uses an asterisk (*) to match any string. You can then reference the string captured by the wildcard in the replacement rule by its position. For example, if the matching rule is/api/* and the replacement rule is/$1, a request to/api/getFunctionName is rewritten to/getFunctionName. The following table provides more examples.

Note In the wildcard rewrite method,* and^ are special characters. All other characters are matched literally.
  • The* character matches any string.
  • The^ character anchors the match to the start of the string.
Matching rule Replacement rule Original URI Rewritten URI
/old /new /old /new
/test/old /new
^/old /new /old /new
/test/old No match
/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 No match

Regex rewrite

The regex rewrite method uses standard regular expression syntax for its matching and replacement rules. For details on the syntax, see RE2 syntax.

Note Regex rewrite is an advanced method with complex syntax for specific use cases. In most scenarios, the wildcard rewrite method is recommended.
Matching rule Replacement rule Original URI Rewritten URI
^/old/[a-z]+/ /new /old/ab/ /new
/test/a-b/ No match
^/api/.+?/(.*) /api/v2/$1 /api/v1/test /api/v2/test
/api/v1-pre/test /api/v2/test

Matching order and priority

The three rewrite methods are prioritized in the following order, from highest to lowest:exact rewrite → wildcard rewrite → regex rewrite.

Function Compute lets you configure multiple rewrite policies for a single route. Therefore, a request may match multiple policies. When this occurs for policies of the same rewrite method, Function Compute evaluates them in their configured order. The first matching policy is applied, and all others are ignored.

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 to/api/v1/getFunctionName is rewritten to/api/v2/getFunctionName.
  • If the rewrite policies are ordered as[rule2, rule1], a request to/api/v1/getFunctionName is rewritten to/api/v3/getFunctionName.

Prerequisites

You have created a service and a function. For more information, see Create a service and Create a function.

Procedure

You can configure a rewrite policy when creating a new custom domain name or add a policy to an existing one.

Configure a policy for a new custom domain

  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, enter the Domain Name, and then under Route Config, click Rewrite Policy.
  4. In the Configure Rewrite Policy panel, click + Add Rewrite Policy. Configure the policy by providing a Matching rule and a Replacement rule for one of the available methods (Exact rewrite, Wildcard rewrite, or Regex rewrite), and then click OK.

Configure a policy for an existing custom domain

  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. Configure the policy by providing a Matching rule and a Replacement rule for one of the available methods (Exact rewrite, Wildcard rewrite, or Regex rewrite), and then click OK.