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 origin requests 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 | Handling method | Scenario |
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. | 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. |
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.
Procedure
Log on to the DCDN console.
In the left-side navigation pane, click Domain Names.
On the Domain Names page, find the domain name that you want to manage and click Configure.
In the left-side navigation tree of the domain name, click Caching.
Click the URL Rewrite tab.
Click Add and configure a rewrite rule based on your business requirements.
Parameter
Description
URI to Be Rewritten
If you set Flag to Break in a rewrite rule, the path must start with a forward slash (
/
) and exclude the protocol and domain name.If you set Flag to Redirect in a rewrite rule, the path can contain the protocol and domain name. Perl Compatible Regular Expressions (PCRE) is supported. Example:
^/hello$
.
Target URI
If you set Flag to Break in a rewrite rule, the path must start with a forward slash (
/
) and exclude the protocol and domain name.If you set Flag to Redirect in a rewrite rule, the path can contain the protocol and domain name. PCRE is supported. For example,
$1
and$2
are used to reference captured strings in parentheses in the path that you want to rewrite.
Flag
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.
Click OK.
After a rewrite rule is created, the rule is displayed on the URL 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
.