All Products
Search
Document Center

CDN:Rewrite origin URLs

Last Updated:Oct 12, 2024

Alibaba Cloud CDN allows you to rewrite origin URLs. The URL rewrite feature functions on points of presence (POPs) without affecting the internal services of Alibaba Cloud CDN or the cache keys.

How it works

POPs match origin URLs against the URLs of the requested resources on the origin server based on URL rewrite rules. Then, the requests with specific parameters are redirected to the origin server.

  • In a rewrite rule, if you set the Flag parameter to None or Break, only the resource path in the URL is rewritten.1

  • If you set the Flag parameter to enhance break, the resource path and query string are rewritten.2

Usage notes

  • You can configure up to 50 Origin URL Rewrite rules for each domain name.

  • The system runs the rewrite rules that are listed on the Origin URL Rewrite tab in order from top to bottom. A change to this order may lead to a different rewrite result.

  • If you set the Flag parameter of an 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 Domain Names > Optimization in the left-side navigation tree of the specified domain name. If you configure the preceding features for the same domain name, make sure that the features do not conflict with each other.Origin URL Rewrite

Differences between access URL rewrite and origin URL rewrite

Feature

Description

Result

Scenario

Rewrite access URLs

The URL that is accessed by clients is rewritten, and the origin URL also changes.

The URL seen by clients changes and is different from the actually accessed URL.

This feature is commonly used to map the URL of the old domain name to a new domain name or provide different URLs for mobile devices and PCs.

Example: When a client accesses old.example.com/hello, rewrite the access URL to new.example.com/hello.

Rewrite origin URLs

The origin URL is rewritten, and the access URL remains unchanged.

The URL seen by clients remains unchanged and is the same as the actually accessed URL.

This feature is commonly used to hide the actual URLs of origin servers to protect information about origin servers. You can also use this feature to map URLs to allow POPs to retrieve content from different origin directories.

Example: When a client accesses cdn.example.com/hello, rewrite the origin URL to origin.example.com/source/hello.

Rewrite an access URL

image
  1. A client initiates a request to a POP. The request URL is old.example.com/hello.

  2. After the POP receives the request, the POP adds the new URL to the HTTP Location header in the response that is returned together with an HTTP status code 302, and rewrites the request URL to new.example.com/hello based on access URL rewrite rules.

  3. After the client receives the response and the HTTP status code 302, the client initiates a request to the new URL.

  4. The POP checks the cache. If the content that corresponds to the rewritten URL exists in the cache, the POP returns the content to the client. If not, the POP initiates a request to the origin server. The request URL is new.example.com/hello, which is rewritten.

  5. The origin server receives the request and returns the requested content to the POP.

  6. The POP caches the requested content and returns the requested content to the client.

Rewrite an origin URL

image
  1. A client initiates a request to a POP. The request URL is cdn.example.com/files/hello.txt.

  2. After the POP receives the request, the POP checks the cache. If the content that corresponds to the request URL exists in the cache, the POP returns the content to the client. If not, the POP rewrites the origin URL to origin.example.com/secret/files/hello.txt based on origin URL rewrite rules and initiate a request to the origin server.

  3. The origin server receives the request and returns the requested content to the POP.

  4. The POP caches the requested content and returns the requested content to the client.

Create an origin URL 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 navigation tree of the domain name, click Origin Fetch.

  5. Click the Origin URL Rewrite tab.

  6. Click Add.

  7. Configure the Path to Be Rewritten, Target Path, and Flag parameters. The following table describes the parameters.

    改写回源URI

    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. You must use Perl Compatible Regular Expressions (PCRE).

    Target Path

    /hello/test

    Enter a URL that starts with a forward slash (/). The URL cannot contain http:// or domain names. PCRE is supported.

    Flag

    None

    If you configure multiple URL rewrite rules, Alibaba Cloud CDN matches requests against the rules in order from top to bottom.

    break

    • If you configure multiple URL rewrite rules, and the current rule is matched, other rules are skipped.

    • This option rewrites only the resource path in the URL. The URL parameters remain unchanged. You can use the parameter rewrite feature to rewrite URL parameters.Origin URL Rewrite

    enhance break

    • If you configure multiple URL rewrite rules, 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 the settings of the feature that is described in Parameter rewrite. 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.

    The new rewrite rule is displayed on the 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

    None

    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 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 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: http://example.com/image/hello.jpg?code=321

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

  • Example 4: Add a URL prefix to the root directory if the file name is a variable

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

    Path to Be Rewritten

    ^(.*)$

    Note

    ^ matches a string from the beginning of the string. (.*) is a group, in which . matches any character except a line feed. * matches the preceding character or group zero or more times. You can use $1 in the target path to reference the variable content of the group. $ matches a string to the end of the string. ^(.*)$ matches a string from the beginning to the end of the string, which can contain any character except a line break, and then captures the matched content into a group. For example, for a string "hello world", ^(.*)$ matches the entire string and captures "hello world" into the first group.

    Target Path

    /image$1

    Note

    /image matches the string "/image". $1 references the content of the first group, $2 references the content of the second group, and so forth. /image$1 matches the string "/image" followed by the content of the first group. For example, if the content of the first group is "abc", /image$1 matches the string "/imageabc". Note that the $1 references the content of the group instead of the literal "$1". If you want to match the literal "$1", use the escaped string "\$1".

    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 other URL rewrite rules that are listed on the Origin URL Rewrite tab.

  • Example 5: Add a URL prefix to a specific directory if the file name is a variable.

    Add the /image path to the URLs of all files in the /live directory. For example, rewrite /live/xxx in URLs to /image/live/xxx. In this example, xxx is a file name, such as hello.jpg or 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

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

  • Example 6: Apply only the rule whose flag is set to None when multiple rules are configured.

    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 the origin URL is rewritten to http://example.com/image_02.png. Then, the request matches the second rule and the origin URL is rewritten to http://example.com/image/image_02.png.

  • Example 7: Apply the rule whose flag is set to Break when multiple rules are configured.

    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_02.png

      Note

      The request matches the first rule and the request URL is rewritten to http://example.com/image_02.png. Other rules are skipped.