If the URI in a request does not match the URI of the cached resource on Dynamic Route for CDN (DCDN) nodes, you can create a URI rewrite rule to overwrite the URI in the request. Then, the request can be redirected to the final URI.

Background information

The HTTP 302 status code, also known as the Found message, indicates that the requested resource has temporarily relocated. After you add a URI rewrite rule, DCDN nodes 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.

After you add a URI rewrite rule, DCDN nodes return the HTTP 302 status code to clients by default 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 Definition Processing method Use 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. 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, the HTTP 307 status code is returned instead of the HTTP 302 status code.

Scenarios

A resource on the origin server is moved to a different URL, and the URL of the resource that is cached on the DCDN nodes is updated. However, a client sends a request to the original URL. In this case, DCDN nodes must rewrite the URL in the request. This way, the request can be redirected to the new URL. For example, an image file is moved from the /download/ directory to the /image/ directory.

URI rewrite

Procedure

Notice You can create up to 50 rewrite rules for a domain name. When multiple rewrite rules are configured, the rules are applied in descending order in which they are listed in the DCDN console.
  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 that you want to manage, and click Configure in the Actions column.
  4. In the left-side navigation pane on the details page of the specified domain name, click Caching.
  5. Click the Rewrite tab.
  6. Click Add. In the Rewrite dialog box, configure the URI to Be Rewritten, Target URI, and Executing Rules parameters.
    Rewrite
    Parameter Description
    URI to Be Rewritten Enter a URI that starts with a forward slash (/). The URI cannot contain http:// or domain names. You can use Perl Compatible Regular Expressions (PCRE) to specify the URI, for example, ^/hello$.
    Target URI Enter a URI that starts with a forward slash (/). The URI cannot contain http:// or domain names. Example: /index.html.
    Executing Rules Redirect and Break are supported.
    • Redirect: If the URI in a request matches the current rule, DCDN returns a 302 status code and the request is redirected to the final URI. After the current rule is executed, the request is matched against other rules.
    • Break: If the URI in a request matches the current rule, DCDN returns a 302 status code and the request is redirected to the final URI. 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.

Configuration examples

The following figure shows how to use a regular expression to configure a URI rewrite rule.Create a rewrite rule

A client sends a request to http://example.aliyundoc.com/hello, and the request contains /hello. DCDN nodes add a new URI http://example.aliyundoc.com/index.html to the Location header in the HTTP 302 response message and return the response message to the client. Then, the client sends a request to http://example.aliyundoc.com/index.html.

Related API operations

BatchSetDcdnDomainConfigs