Alibaba Cloud CDN allows you to rewrite URIs in back-to-origin requests. The URI rewrite feature functions on CDN edge nodes without affecting the internal services of Alibaba Cloud CDN or the cache keys. This topic describes how to configure URI rewrite.

Scenarios

If a resource on the origin server relocates, the URI of the resource also changes. When user requests use the original URI, Alibaba Cloud CDN must rewrite the URI to redirect the requests to the new URI.

For example, images may relocate from the directory /download/ to /image/.

How it works

CDN edge nodes match request URIs against those of the requested resources on the origin server based on URI rewrite rules. Then, the requests or specific request parameters are redirected to the origin server.
  • In a rewrite rule, if you set Flag to None or Break, only the resource URI is rewritten. ziyuanlujing
  • If you set Flag to enhance break, both the resource URI and query string are rewritten. ziyuanlujing2

Usage notes

  • Each domain name supports up to 50 URI rewrite rules.
  • The system runs the rewrite rules that are listed on the URI Rewrite tab in order from top to bottom. A change to this order may lead to a different rewrite result.
  • The URI rewrite feature is different from the rewrite feature on the Cache page. The rewrite feature functions on CDN edge nodes. It affects the internal services of Alibaba Cloud CDN, and rewrites cache keys. The URI rewrite feature functions on the CDN edge nodes that communicate with origin servers. It does not affect the internal services of Alibaba Cloud CDN or rewrite cache keys.
  • If you set the flag of a URI rewrite rule to enhance break, the parameter rewrite settings may conflict with the settings of the parameter filtering (retain specified parameters) or parameter filtering (delete specified parameters) feature on the Domain Names > Optimization page. If you want to enable all of the three features, make sure that the settings do not conflict with each other.

Create a URI rewrite rule

  1. Log on to the Alibaba Cloud CDN console.
  2. In the left-side navigation pane, click Domain Names.
  3. On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column.
  4. In the left-side management pane of the domain name, click Back-to-origin.
  5. Click the URI Rewrite tab.
  6. Click Add.
  7. In the URI Rewrite dialog box, set the Original URI, Final URI, and Flag parameters.
    Create a URI rewrite rule
    Parameter Example Description
    Original URI ^/hello$ Enter a URI that starts with a forward slash (/). The URI cannot contain the string http:// or domain names. Perl Compatible Regular Expressions (PCRE) are supported.
    Final URI /hello/test Enter a URI that starts with a forward slash (/). The URI cannot contain the string http:// or domain names.
    Flag None If multiple URI rewrite rules are set, Alibaba Cloud CDN matches requests against the rules in order from top to bottom.
    break
    • If multiple URI rewrite rules are set, and the current rule is matched, the other rules are skipped.
    • Rewrites only the URI part. The URL parameters remain unchanged. You can use the parameter rewrite feature to rewrite URL parameters.
    enhance break
    • If multiple rules are set, and the current rule is matched, the other rules are skipped.
    • Compared with Break, enhance break also rewrites URL parameters. However, the parameter rewrite settings may conflict with those of the parameter rewrite feature. If you want to enable both features, make sure that the settings do not conflict with each other.
  8. Click OK to apply the rule.

    To modify or delete a rewrite rule, find the rule on the URI Rewrite tab and click Modify or Delete in the Actions column.

Configuration examples

  • Example 1: Apply a rule whose flag is set to None.
    Original URI ^/hello$
    Final URI /index.html
    Flag None
    Expected result Original request: http://example.com/hello

    Final request: http://example.com/index.html

    The system continues to match the request against the other URI rewrite rules that are listed on the URI Rewrite tab.

  • Example 2: Apply a rule whose flag is set to Break.
    Original URI ^/hello.jpg$
    Final URI /image/hello.jpg
    Flag break
    Expected result Original request: http://example.com/hello.jpg

    Final request: http://example.com/image/hello.jpg

    The system stops matching the request against the other URI rewrite rules that are listed on the URI Rewrite tab.

  • Example 3: Apply a rule whose flag is set to enhance break.
    Original URI ^/hello.jpg?code=123$
    Final URI /image/hello.jpg?code=321
    Flag enhance break
    Expected result Original request: http://example.com/hello.jpg?code=123

    Rewritten request: http://example.com/image/hello.jpg?code=321

    The system stops matching the request against the other URI rewrite rules that are listed on the URI Rewrite tab.
  • Example 4: Add a URI prefix to the root directory when the file name is a variable

    Add the /image path to the URI of all files in the root directory. For example, you can rewrite /xxx in URIs to /image/xxx. In this example, xxx is a file name, such as /hello.jpg and /hello.html.

    Original URI ^(.*)$
    Note ^(.*)$ matches any characters, and a pair of parentheses () represents a group. You can use $1 in the final URI to call variables in the group.
    Final URI /image$1
    Note $1, the first special replacement pattern, represents the content that matches the expression in the first pair of parentheses () in a regular expression. $2, the second special replacement pattern, represents the content that matches the expression in the second pair of parentheses (). $n represents the nth special replacement pattern.
    Flag break
    Expected result
    • Original request: http://example.com/hello.jpg

      Final request: http://example.com/image/hello.jpg

    • Original request: http://example.com/hello.html

      Final request: http://example.com/image/hello.html

    The system stops matching the request against the other URI rewrite rules that are listed on the URI Rewrite tab.
  • Example 5: Add a URI prefix to a specified directory when the file name is a variable.
    Add the /image path to the URI of all files in the /live directory. For example, you can rewrite /live/xxx in URIs to /image/live/xxx. In this example, xxx is a file name, such as /live/hello.jpg and /live/hello.html.
    Original URI ^/live/(.*)$
    Final URI /image/live/$1
    Flag break
    Expected result
    • Original request: http://example.com/live/hello.jpg

      Final request: http://example.com/image/live/hello.jpg

    • Original request: http://example.com/live/hello.html

      Final request: http://example.com/image/live/hello.html

    The system stops matching the request against the other URI rewrite rules that are listed on the URI Rewrite tab.
  • Example 6: Apply only the rule whose flag is None when multiple rules are set.
    Create two URI rewrite rules, as shown in the following figure.Settings
    Expected result:
    • Original request: http://example.com/image_01.pn
    • Final Request: http://example.com/image/image_02.png
      Note The request matches the first rule and is rewritten to http://example.com/image_02.png. Then, the request matches the second rule and is rewritten to http://example.com/image/image_02.png.
  • Example 7: Apply the rule whose flag is Break when multiple rules are set.
    Create two URI rewrite rules, as shown in the following figure.Settings
    Expected result:
    • Original request: http://example.com/image_01.png
    • Final Request: http://example.com/image_02.png
      Note The request matches the first rule and is rewritten to http://example.com/image_02.png. The other rules are skipped.