Origin URL rewrite

更新时间:
复制 MD 格式

ApsaraVideo VOD allows you to rewrite the URL of a back-to-origin request without affecting internal CDN pathways or the cache key. A point of presence (POP) uses the rewritten URL only when it sends a back-to-origin request to the origin.

How it works

Origin Path Rewrite rules let you modify the request path to match the actual resource location on your origin server. This ensures that POPs can accurately retrieve resources or pass specific query strings to the origin.

  • If the Flag is set to None or break, only the resource path in the URL is rewritten.

    无标题文档-流程图

  • If the Flag is set to enhance break, both the resource path and the query string can be rewritten.

    zh-enhance_break

Usage notes

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

  • Rules are applied from top to bottom. Their order affects the final rewrite result.

  • When you configure an execution rule for origin URL rewrite, rewriting URL parameters may conflict with the Ignore Parameters feature on the Domain Names > Performance Optimization tab. If you configure both features, make sure their settings do not conflict.

Procedure

  1. Log on to the ApsaraVideo VOD console.

  2. In the left-side navigation pane, choose Configuration Management > CDN Configuration > Domain Names.

  3. Find the domain name that you want to manage and click Configure in the Actions column.

  4. In the left-side navigation pane for the domain name, click Back-to-Origin.

  5. Click the Origin URL Rewrite tab.

  6. Click Add and configure the source path, destination path, and execution rule based on your requirements.

    Important

    Rewrite rules on the Origin URL Rewrite page are executed sequentially from top to bottom, which can affect your rewrite results.

    Parameter

    Example

    Description

    Source Path

    ^/hello$

    The URL path to match. It must start with a forward slash (/) and must not include the http:// prefix or a domain name. Perl Compatible Regular Expressions (PCRE) are supported.

    Destination Path

    /hello/test

    The URL path to use for the rewrite. It must start with a forward slash (/) and must not include the http:// prefix or a domain name.

    execution rule

    Empty

    If a request URL matches the rule, the system applies the rewrite and continues processing. The rewritten URL is then evaluated against subsequent rules.

    break

    • If a request URL matches this rule, the system executes the rule and stops processing subsequent rules.

    • This option rewrites only the resource path in the URL, not URL parameters. It does not conflict with the parameter rewrite feature.

    enhance break

    • If a request URL matches this rule, the rule is executed and no subsequent rules are processed.

    • Similar to break, but this option can also rewrite URL parameters. .

  7. Click OK to apply the rewrite rule.

    On the Origin URL Rewrite tab, you can also click Modify or Delete in the rule list to manage existing rewrite rules.

Configuration examples

Example 1: Empty execution rule

Path to Be Rewritten

^/hello$

Target Path

/index.html

Flag

None

Result

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

Origin fetch request after rewrite: http://example.com/index.html

The request is then matched against subsequent rules in the Origin Path Rewrite list.

Example 2: Break execution rule

Path to Be Rewritten

^/hello.jpg$

Target Path

/image/hello.jpg

Flag

break

Result

Original request: http://example.com/hello.jpg

Origin fetch request after rewrite: http://example.com/image/hello.jpg

No subsequent rules in the Origin Path Rewrite list are evaluated.

Example 3: Enhance break execution rule

Path to Be Rewritten

^/hello.jpg?code=123$

Target Path

/image/hello.jpg?code=321

Flag

enhance break

Result

Original request: http://example.com/hello.jpg?code=123

Origin fetch request after rewrite: http://example.com/image/hello.jpg?code=321

No subsequent rules in the Origin Path Rewrite list are evaluated.

Example 4: Add a prefix to root paths

For example, you can rewrite a URL path like /xxx, where xxx is a variable filename (e.g., /hello.jpg or /hello.html), to /image/xxx. This inserts the /image prefix into the path for any file in the root directory.

Path to Be Rewritten

^(.*)$

Note

^ matches the start of the string. (.*) is a capturing group, where . matches any single character except for a newline character, and * matches the preceding character or group zero or more times. You can use $1 in the Target Path to reference the content captured by this group. $ matches the end of the string. Therefore, ^(.*)$ matches the entire string from start to end and captures all characters, except for newline characters, into a group. For example, for the 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 capture group, $2 references the content of the second capture group, and so on. Therefore, /image$1 means that the string "/image" is immediately followed by the content of the first capture group. For example, if the content of the first capture group is "abc", /image$1 matches the string "/imageabc". Note that $1 references the content of the capture group, not the literal string "$1". To match the literal string "$1", you must use an escape character, such as "\$1".

Flag

break

Result

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

    Origin fetch request after rewrite: http://example.com/image/hello.jpg

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

    Origin fetch request after rewrite: http://example.com/image/hello.html

No subsequent rules in the Origin Path Rewrite list are evaluated.

Example 5: Add a prefix to specified paths

For example, you can rewrite a URL that contains /live/xxx, where xxx represents any filename (e.g., hello.jpg or hello.html), to /image/live/xxx. This inserts the /image prefix into the path for any file under the /live directory.

Path to Be Rewritten

^/live/(.*)$

Target Path

/image/live/$1

Flag

break

Result

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

    Origin fetch request after rewrite: http://example.com/image/live/hello.jpg

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

    Origin fetch request after rewrite: http://example.com/image/live/hello.html

No subsequent rules in the Origin Path Rewrite list are evaluated.

Example 6: Matching multiple rules with the empty rule

Consider two enabled rules. Rule 1: Source Path is ^/image_01.png$, Destination Path is /image_02.png, and execution rule is Empty. Rule 2: Source Path is ^(.*)$, Destination Path is /image$1, and execution rule is Empty.

Result:

  • Original request: http://example.com/image_01.png

  • Origin fetch request after rewrite: http://example.com/image/image_02.png

    Note

    The request first matches Rule 1 and is rewritten to http://example.com/image_02.png. Because the Flag is set to None, the rewritten path is then evaluated against Rule 2, which also matches. The path is rewritten a second time, resulting in the final path: http://example.com/image/image_02.png.

Example 7: Matching multiple rules with the break rule

Consider two enabled rules. Rule 1: Source Path is ^/image_01.png$, Destination Path is /image_02.png, and execution rule is break. Rule 2: Source Path is ^(.*)$, Destination Path is /image$1, and execution rule is Empty.

Result:

  • Original request: http://example.com/image_01.png

  • Origin fetch request after rewrite: http://example.com/image_02.png

    Note

    The request first matches Rule 1 and is rewritten to http://example.com/image_02.png. Because the Flag for Rule 1 is set to break, processing stops and no subsequent rules are evaluated.

Related topics

Batch configure domain names