Updated on 2024-04-03 GMT+08:00

Access URL Rewrite

Set access URL rewrite rules to redirect user requests to the URLs of cached resources.

Scenario

If the path for storing server resources changes, the path for storing resources on CDN PoPs changes accordingly. For example, the path of an image has changed from /test to /testnew. If a user sends a request to the original URL, CDN PoPs need to rewrite the requested URL.

If a URL rewrite rule has been configured, CDN matches URLs in redirection mode and uses the HTTP 302 status code (302 Found) to indicate that the requested resource has been temporarily moved to another location. Specifically, CDN PoPs add the new URL to the HTTP Location header in the 302 response sent to the client. After receiving the response, the client sends a request to the new URL. Table 1 lists the redirection status codes and their meanings.

Table 1 Redirection modes

Code

Meaning

Processing Method

Description

301

Moved Permanently

The GET method does not change. Other methods may change to the GET method.

The resource is moved permanently.

302

Found

The GET method does not change. Other methods may change to the GET method.

This page is temporarily unavailable for unforeseen reasons.

303

See Other

The GET method does not change. Other methods are changed to the GET method (the message body is lost).

Used to redirect after a PUT or a POST, so that refreshing the result page does not re-trigger the operation.

307

Temporary Redirect

Neither the method nor the message body changes.

This page is temporarily unavailable for unforeseen reasons. Better than 302 when non-GET operations are available on the site.

Procedure

  1. Log in to Huawei Cloud console. Choose Service List > Content Delivery & Edge Computing > Content Delivery Network.

    The CDN console is displayed.

  2. In the navigation pane, choose Domains.
  3. In the domain list, click the domain name you want to modify or click Configure in the Operation column.
  4. Click the Cache Settings tab.
  5. In the Access URL Rewrite area, click Add.
    Figure 1 Adding an access URL rewrite rule
    Table 2 Parameters

    Parameter

    Description

    Content Type

    • Directory: Execute the rule for files in the specified directory.
    • Full path: Execute the rule for the file of the specified path.

    Content

    • When Content Type is set to Directory, enter up to 20 directories and separate them by commas (,). A directory starts with a slash (/). Wildcards (*) are not supported. Example: /test/folder01,/test/folder02
    • When Content Type is set to Full path, enter the path of a file or a path with wildcards (*). A path must start with a slash (/) and cannot end with a wildcard (*). Only one wildcard (*) can be entered. Examples: /test/index.html or /test/*.jpg
    • Regular expression match is not supported.

    Redirection URL

    Target URL. Start with a slash (/) and do not contain http://, https://, or the domain name. Example: /newtest/index.html

    • When Content Type is set to Full path, the wildcard (*) can be captured by $1. For example, if Content is set to /test/*.jpg and Redirection URL is set to /newtest/$1.jpg, when a user requests /test/11.jpg, $1 is replaced by 11, so the requested URL after redirection is /newtest/11.jpg.

    Action

    Select Redirect or Break.

    • Redirect: If the requested URL matches this rule, the request is redirected to the target URL. After this rule is executed, if other rules exist, CDN continues to execute these rules.
    • Break: If the requested URL matches this rule, the request is redirected to the target URL. After this rule is executed, CDN does not execute any other rules and returns status code 200. You cannot set the status code.

    Redirection Host

    Domain name to which client requests are redirected. Specify the value when Action is set to Redirect.

    • By default, the acceleration domain name is used.
    • The value contains 1 to 255 characters and starts with http:// or https://, for example, http://www.example.com.

    Redirection Status Code

    Specify the value when Action is set to Redirect. The status code can be 301, 302, 303, or 307. For details about their differences, see Table 1.

    Priority

    Priority of the rule. Enter an integer ranging from 1 to 100. A greater number indicates a higher priority.

    Each rule must have a unique priority.

  6. Click OK.