All Products
Search
Document Center

CDN:Rewrite origin path

Last Updated:Jan 04, 2026

Alibaba Cloud CDN allows you to rewrite origin paths. 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 paths 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

Notes

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

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

  • Origin Path Rewrite may conflict with the settings of the Ignore Parameters feature in Domain Names>Optimization. Make sure that the features do not conflict with each other.

Differences between access URL rewrite and origin path rewrite

Feature

Affected

Client experience

Use case

Rewrite access URLs

Affects the URL that the client accesses. It also changes the URL that the POP uses for origin fetch.

  • If the flag is set to redirect, the client uses the redirected URL to send a new request.

  • If the flag is set to break, the URL that the client sees is the same as the accessed URL and does not change.

Commonly used to migrate or map URLs from an old domain name to a new domain name, or to provide different URLs for mobile and PC clients.

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

Rewrite origin path

Affects the URL that the POP uses for origin fetch. The URL that the client accesses does not change.

The URL that the client sees is the same as the accessed URL and does not change.

Commonly used to hide the actual URL structure of the origin server to protect origin server information, or to use URL mapping to allow CDN POPs to fetch content from different origin directories.

Example: When a client accesses cdn.example.com/hello, the origin path is rewritten to origin.example.com/source/hello.

Access URL rewriting

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

  2. After the POP receives the request, it rewrites the request URL to new.example.com/hello based on the access URL rewrite rule. The POP then includes the new URL in the HTTP Location header of the 302 response.

  3. After the client receives the 302 response, it sends a new request to the new URL.

  4. The POP checks its cache. If the content for the rewritten URL is cached, the POP returns the content to the client. If the content is not cached, the POP sends a request to the origin server. The request URL is the rewritten URL, new.example.com/hello.

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

  6. The POP caches the response and returns it to the client.

Origin path rewriting

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

  2. After the POP receives the request, it checks its cache. If the content for the request URL is cached, the POP returns the content to the client. If the content is not cached, the POP rewrites the origin path to origin.example.com/secret/files/hello.txt based on the origin path rewrite rule and sends a request to the origin server.

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

  4. The POP caches the response and returns it to the client.

Create an origin path rewrite rule

  1. Log on to the CDN console.

  2. In the left navigation pane, click Domain Names.

  3. On the Domain Names page, find the target domain name 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 Path 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 Path 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 Rewrite origin fetch parameters. 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 Path 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 Path 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 Path 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 Path 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 path 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 path 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 path is rewritten to http://example.com/image_02.png. Then, the request matches the second rule and the origin path 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.