All Products
Search
Document Center

CDN:Create an access URL rewrite rule

Last Updated:Aug 28, 2023

If the URL that is requested by a client does not match the URL of the cached resource on the Alibaba Cloud CDN points of presence (POPs), you can configure the access URL rewrite feature to redirect the request to the actual path.

Background information

The HTTP status code 302, also known as the Found message, indicates that the requested resource has temporarily relocated. After you configure the access URL rewrite feature, Alibaba Cloud CDN POPs can add the new URL of the resource to the Location header in the response message of the HTTP status code 302. After the client receives the message, the client sends a request to the new URL.

After you create an access URL rewrite rule, the Alibaba Cloud CDN POPs return the HTTP status code 302 to the client if the requested resource has relocated. You can also set the HTTP status code to 303 or 307. To change the HTTP status code, submit a ticket.

HTTP status code

Description

Solution

Scenarios

302

Found

GET requests remain unchanged. Requests that use other methods may be changed to GET requests.

A web page is temporarily inaccessible due to unknown reasons. In this case, search engines do not update the URL to the web page.

303

See Other

GET requests remain unchanged. Requests that use other methods are changed to GET requests. The message body is dropped.

Redirect web pages after PUT or POST requests are processed to prevent frequent requests that are initiated by a page refresh.

307

Temporary Redirect

The request method and message body remain unchanged.

A web page is temporarily inaccessible due to unknown reasons. In this case, search engines do not update the URL to the web page. If the website supports requests that use methods other than GET, the HTTP 307 status code is returned instead of the HTTP 302 status code.

Important

You can create up to 50 rewrite rules for a domain name. If you create multiple access rewrite rules, the rules are applied in descending order in which they are listed in the Alibaba Cloud CDN console.

Scenarios

A resource on the origin server is moved to a different URL, and the URL of the resource that is cached on Alibaba Cloud CDN POPs is updated. However, a client sends a request to the original URL. In this case, Alibaba Cloud CDN must rewrite the request to redirect the request to the new URL. For example, an image file is moved from the /download/ directory to the /image/ directory. For example, an image file is moved from the /download/ directory to the /image/ directory.

URI重写规则

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 domain name that you want to manage and click Manage in the Actions column.
    Domain Names
  4. In the left-side navigation pane of the domain name, click Cache.
  5. Click the Access URL Rewrite tab.

  6. Click Create and configure the parameters based on your business requirements.

    重写

    Parameter

    Description

    Path to Be Rewritten

    Enter a URL that starts with a forward slash (/). The URL cannot contain http:// or domain names. You can use Perl Compatible Regular Expressions (PCRE) to specify the URL. Example: ^/hello$.

    Target Path

    Enter a URL that starts with a forward slash (/). The URL cannot contain http:// or domain names. Example: /index.html.

    Flag

    • By default, Redirect and Break are supported.

      • Redirect: If the URL in a request matches a rule, Alibaba Cloud CDN returns the HTTP 302 status code and the request is redirected to the actual URL. The value of the Location header in the response message that is returned by the POP to the client is the actual URL. Parameters in the original URL are not modified. After the current rule is executed, the request is matched against other rules.

      • Break: If the URL in a request matches a rule, the requested URL is overwritten by the actual URL. Parameters in the original URL are not modified. After the current rule is executed, other rules are skipped.

    • Empty, enhance-break, and enhance_redirect are also supported. To use these rules, submit a ticket.

      • Empty: If multiple rules are configured and the URL in a request matches a rule, subsequent rules are matched after the current rule is executed.

      • enhance_break: enhance_break is similar to break, but modifies the URL, including parameters.

      • enhance_redirect: enhance_redirect is similar to redirect, but modifies the URL, including parameters.

    Note

    Different rules use different rewrite methods, and whether the rewritten URL supports other domain names and other protocols also vary.

    • Empty, Break, and enhance_break rewrite the URL in a request, but cannot modify the domain name or protocol of the URL. For example, the rules cannot modify the protocol of the URL from HTTP to HTTPS.

    • Redirect and enhance_redirect use 302 redirection to rewrite URL, and can modify the domain name or protocol of the URL.

      • You can set the Location header in 302 redirection requests to the current domain name or another domain name. This way, the original URL uses the example.com domain name, and the rewritten URL uses the aliyundoc.com domain name.

      • The Location header in 302 redirection requests supports different protocols

    Rule Condition

    Rule conditions can identify parameters in a request to determine whether a configuration takes effect on the request.

    • Do not use conditions

    • Select the configured rule conditions in Rules Engine. For more information, see Rules engine.

    Enable NGINX variable computing

    By default, the check box is not selected. If you select this check box, you can use NGINX built-in variables in the target URL. Sample configurations:

    • Path to be rewritten: ^/test.jpg$

    • Target path: /test.${arg_type}

    • After you enable NGINX variable calculation, the value of ${nginx_var} is calculated. ${arg_type} indicates the value of the type parameter in the original URL.

    Note

    To configure this parameter, submit a ticket.

  7. Click OK.

    After a rewrite rule is created, the rule is displayed on the Access URL Rewrite tab. You can modify or delete the rule.

Sample configuration

The following figure shows how to use a regular expression to configure an access URL rewrite rule. 正则表达

If a client requests http://example.aliyundoc.com/hello, which contains /hello, the POP writes a new URL http://example.aliyundoc.com/index.html to the Location header and returns the HTTP status code 302 to the client. The client initiates a request to http://example.aliyundoc.com/index.html.

Related API operations

BatchSetCdnDomainConfig