Alibaba Cloud CDN allows you to rewrite URLs in back-to-origin requests. The URL rewrite feature functions on CDN points of presence (POPs) without affecting the internal services of Alibaba Cloud CDN or the cache keys. This topic describes how to configure Back-to-origin URL 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
- In a rewrite rule, if you set Flag to None or Break, only the resource path in the URL is rewritten.
- If you set Flag to enhance break, both the resource path and request parameters are rewritten.
Usage notes
- You can configure up to 50 back-to-origin URL rewrite rules for each domain name.
- The system runs the rewrite rules that are listed on the Back-to-origin URL Rewrite tab in order from top to bottom. A change to this order may lead to a different rewrite result.
- The Back-to-origin URL Rewrite feature is different from the Access URL Rewrite feature on the Cache page. The Access URL Rewrite feature functions on POPs that distribute content to users. It affects the internal services of Alibaba Cloud CDN, and rewrites cache keys. The Back-to-origin URL Rewrite feature functions on the POPs 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 back-to-origin URL rewrite rule to enhance break, the parameter rewrite settings may conflict with the settings of the Ignore Parameters feature. To ignore parameters, choose in the left-side navigation pane of the Alibaba Cloud CDN console. If you configure these features for the same domain name, make sure that the features do not conflict with each other.
Create a back-to-origin URL rewrite rule
- Log on to the Alibaba Cloud CDN console.
- In the left-side navigation pane, click Domain Names.
- On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column.
- In the left-side management pane of the domain name, click Back-to-origin.
- Click the Back-to-origin URL Rewrite tab.
- Click Add.
- Configure Path to Be Rewritten, Target Path, and Flag.
Parameter Example Description Path to Be Rewritten ^/hello$ Enter a URL that starts with a forward slash (/). The URL cannot contain http:// or domain names. Perl Compatible Regular Expressions (PCRE) are supported. Target Path /hello/test Enter a URL that starts with a forward slash (/). The URL cannot contain http:// or domain names. Flag Empty If multiple URL rewrite rules are set, Alibaba Cloud CDN matches requests against the rules in order from top to bottom. break - If multiple URL rewrite rules are set, and the current rule is matched, other rules are skipped.
- Rewrites only the resource path in the URL. The URL parameters remain unchanged. You can use the parameter rewrite feature to rewrite URL parameters.
enhance break - If multiple URL rewrite rules are set, and the current rule is matched, other rules are skipped.
- Compared with Break, enhance break also rewrites URL parameters. However, the parameter rewrite settings may conflict with those of the feature that is described in Rewrite URL parameters in back-to-origin requests. If you want to enable both features, make sure that the settings do not conflict with each other.
- Click OK to apply the rule.
The newly configured rewrite rule appears on the Back-to-origin URL Rewrite tab. You can click Modify or Delete in the Actions column of the rewrite rule to modify or delete the rewrite rule.
Configuration examples
- Example 1: Apply a rule whose flag is set to None.
Path to Be Rewritten ^/hello$ Target Path /index.html Flag Empty Expected result Original request: http://example.com/hello
Final request:
http://example.com/index.html
The system continues to match the request against other URL rewrite rules that are listed on the Back-to-origin URL Rewrite tab.
- Example 2: Apply a rule whose flag is set to Break.
Path to Be Rewritten ^/hello.jpg$ Target Path /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 other URL rewrite rules that are listed on the Back-to-origin URL Rewrite tab.
- Example 3: Apply a rule whose flag is set to enhance break.
Path to Be Rewritten ^/hello.jpg?code=123$ Target Path /image/hello.jpg?code=321 Flag enhance break Expected result Original request: http://example.com/hello.jpg?code=123
Final request:
The system stops matching the request against other URL rewrite rules that are listed on the Back-to-origin URL Rewrite tab.http://example.com/image/hello.jpg?code=321
- Example 4: Add a URL prefix to the root directory when the file name is a variable
Add the /image path to the URLs of all files in the root directory. For example, you can rewrite /xxx in URLs to /image/xxx. In this example, xxx is a file name, such as hello.jpg and hello.html.
Path to Be Rewritten ^(.*)$ Note ^(.*)$ matches any characters, and a pair of parentheses () represents a group. You can use $1 in the target path to call variables in the group.Target Path /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
- Original request:
- Example 5: Add a URL prefix to a specified directory when the file name is a variable. Add the /image path to the URLs of all files in the /live directory. For example, you can rewrite /live/xxx in URLs to /image/live/xxx. In this example, xxx is a file name, such as hello.jpg and hello.html.
Path to Be Rewritten ^/live/(.*)$ Target Path /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
- Original request:
- Example 6: Apply only the rule whose flag is None when multiple rules are set. Create two URL rewrite rules, as shown in the following figure.Expected result:
- Original request:
http://example.com/image_01.png
- Final request:
http://example.com/image/image_02.png
Note The request matches the first rule and is rewritten tohttp://example.com/image_02.png
. Then, the request matches the second rule and is rewritten tohttp://example.com/image/image_02.png
.
- Original request:
- Example 7: Apply the rule whose flag is Break when multiple rules are set. Create two URL rewrite rules, as shown in the following figure.Expected result:
- Original request:
http://example.com/image_01.pn
- Final request:
http://example.com/image_02.png
Note The request matches the first rule and is rewritten tohttp://example.com/image_02.png
. Other rules are skipped.
- Original request: