All Products
Search
Document Center

Dynamic Content Delivery Network:Create a URI rewrite rule

Last Updated:Feb 29, 2024

If a resource on the origin server is relocated, the URL of the resource that is cached on Dynamic Content Delivery Network (DCDN) points of presence (POPs) is accordingly adjusted. If a user request carries an outdated URL, POPs rewrite the URL and redirect the request to the new URL. This reduces the number of requests that are sent to the origin server and enhances user access performance.

Background information

The HTTP status code 302, also known as the Found message, indicates that the requested resource has been temporarily relocated. After you create a URI rewrite rule, DCDN POPs can add the new URI of the resource to the Location header in the HTTP 302 response message. After the client receives the message, the client sends a request to the new URI.

In addition to the default status code 302, POPs also support status codes 303 and 307. If you want to change the redirect status code, submit a ticket.

HTTP status code

Description

Processing method

Scenario

302

Found

GET 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.

303

See Other

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

This status code is used to redirect PUT and POST requests to prevent frequent redirections that are caused by a page refresh.

307

Temporary Redirect

Both 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, status code 307 is returned instead of status code 302.

Important

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

Scenarios

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

URI重写

Procedure

  1. Log on to the DCDN console.

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

  3. On the Domain Names page, find the domain name whose acceleration region you want to change and click Configure.
  4. In the left-side navigation tree of the domain name, click Caching.

  5. Click the Rewrite tab.

  6. Click Add and configure a rewrite rule based on your business requirements.

    重写

    Parameter

    Description

    URI to Be Rewritten

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

    Target URI

    Enter a URI. The URI must start with a forward slash (/) and cannot contain http:// or domain names. Example: /index.html.

    Executing Rules

    Valid values: Redirect and Break.

    • Redirect: If the URI in a request matches a specified rule, a POP returns status code 302 and redirects the request to the URI that is indicated by the Location header returned by the POP to clients. Parameters in the original URI are not modified. After the current rule is executed, the request is matched against other rules.

    • Break: If the URI in a request matches a specified rule, a POP redirects the request to the target URI. Parameters in the original URI are not modified. 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 Rewrite tab. You can modify or delete the rule.

Sample configuration

The following figure shows how to use a regular expression to configure a URI rewrite rule. 重写设置

When a client requests http://example.aliyundoc.com/hello, which contains /hello. The nearest DCDN POP returns status code 302 and writes the URI http://example.aliyundoc.com/index.html to the Location header in the response. Then, the client sends a request to http://example.aliyundoc.com/index.html.

Related API operations

BatchSetDcdnDomainConfigs