If the URL requested by a client does not match the URL of the cached resource on the 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, 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 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 codeDescriptionProcessing methodUse scenario
302FoundGET requests remain unchanged. Requests that use other request methods may be changed to GET. A web page is temporarily inaccessible due to unknown reasons. In this case, search engines do not update the URL to the web page.
303See OtherGET requests remain unchanged. Requests that use other request methods are changed to GET. The message body is dropped. Redirect PUT and POST requests to prevent frequent redirections that are caused by a page refresh.
307Temporary RedirectBoth 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 request 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 multiple access rewrite rules are set, they are applied in descending order in which they are listed in the CDN console.

Common scenario

A resource on the origin server is moved to a different URL, and the URL of the resource that is cached on the CDN POPs is updated. However, a client sends a request to the original URL. In this case, 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.

URL rewrite rule

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 Path to Be Rewritten, Target Path, and Flag based on your business requirements.
    Rewrite
    ParameterDescription
    Path to Be RewrittenEnter 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 PathEnter a URL that starts with a forward slash (/). The URL cannot contain http:// or domain names. Example: /index.html.
    FlagRedirect and Break are supported.
    • Redirect: If the URL in a request matches the current rule, Alibaba Cloud CDN returns the HTTP 302 status code and the request is redirected to the actual path. After the current rule is executed, the request is matched against other rules.
    • Break: If the URL in a request matches the current rule, the requested URL is overwritten by the actual path. After the current rule is executed, other rules are skipped.
  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.

Configuration examples

The following figure shows how to use a regular expression to configure an access URL rewrite rule.Use a regular expression

If a client requests http://example.aliyundoc.com/hello, which contains /hello, the CDN 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