If you need to change the URI of the access requests sent to the origin, you can create rules to rewrite URIs. This topic describes how to create URI rewrite rules in the CDN console.

Background information

When a request URI does not match the URI of the requested resource on the origin server, you must change the request URI. You can create multiple rewrite rules as needed.

Procedure

  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 target domain name and click Manage.
  4. In the left-side navigation pane of the specified domain name, click Back-to-origin.
  5. Click the URI Rewrite tab.
  6. On the URI Rewrite tab, click Add.
  7. In the URI Rewrite dialog box that appears, specify the source URI, the target URI, and the flag.
    Flag Description
    None If multiple rules are created, the system continues to match rules after this rule is matched.
    break If multiple rules are created, the system stops matching rules after this rule is matched. In addition, only the request URI is rewritten. The parameters following the question mark (?) are not rewritten.
    enhance_break If multiple rules are created, the system stops matching rules after this rule is matched. In addition, both the URI and the parameters following the question mark (?) are rewritten.
    4
    Notice
    • If you set the flag of a URI Rewrite rule to break, the query parameters in the request URL will not be rewritten. However, the Parameter Rewrite feature still takes effect.
    • 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 Rewrite feature. If you configure both features at the same time, make sure that no conflicts exist.
    • If you set the flag of a URI Rewrite rule to enhance_break, the parameter rewrite settings may conflict with the settings of the Retain Parameters or Ignore Parameters feature on the Domain Names > Optimization page. If you configure these three features at the same time, make sure that no conflicts exist.
  8. Click OK to apply and run the rewrite 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.

    Notice
    • A domain supports up to 50 URI rewrite rules.
    • The system runs the listed rewrite rules 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 at the CDN edge nodes, which affects the internal links of CDN and rewrites the Cache Key. The URI Rewrite feature functions at the CDN nodes that communicate with the origin, which does not affect the internal links of CDN or rewrite the Cache Key.

Example

Example 1

Source URI ^/hello$
Target URI /index.html
Flag None
Description Original request: http://domain.com/hello

Rewritten request: http://domain.com/index.html

The system will continue to match this request against the subsequent URI rewrite rules in the list.

Example 2

Source URL ^/hello.jpg$
Target URI /image/hello.jpg
Flag break
Description Original request: http://domain.com/hello.jpg

Rewritten request: http://domain.com/image/hello.jpg

The system will stop matching this request against the subsequent URI rewrite rules in the list.

Example 3

Source URI ^/hello.jpg\?code=123$
Target URI /image/hello.jpg?code=321
Flag enhance_break
Description Original request: http://domain.com/hello.jpg?code=123

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

The system will stop matching this request against the subsequent URI rewrite rules in the list.