All Products
Search
Document Center

CDN:Configure 301/302 redirection

Last Updated:Nov 09, 2023

The 301/302 redirection feature supports HTTP 301 and 302 status codes that are returned by the origin server. All features that are supported by the HTTP 302 status code in this topic are supported by the HTTP 301 status code. After you configure 301/302 redirection, points of presence (POPs) process the HTTP 301 or 302 status code that is returned by the origin server instead of returning the status code to clients. This simplifies request processing and accelerates content delivery.

Prerequisites

301/302 redirection is enabled on the origin server.

Background information

301 and 302 are HTTP status codes, which indicate that an existing resource is relocated and the request failed to access the resource. In most cases, servers add the Location header to the response header to resolve this issue. After a client receives a response that carries the HTTP 301 or 302 status code and the Location header, the request is redirected to the address that is specified by the Location header to retrieve the resource.

How it works

If a POP redirects a request to an origin server and receives the HTTP 301 or 302 status code from the origin server, the POP processes the status code. The request is redirected to the address that is specified by the Location header returned from the origin server to retrieve the resource. In this case, the HTTP 301 or 302 status code is not returned to the client.

image

  1. A user requests http://example.com/examplefile.txt from a POP.

  2. The requested file is not cached on the POP. The POP redirects the request to the origin server.

  3. The origin server receives the request and returns the HTTP 301 or 302 status code. The response header carries the Location header that specifies the address http://www.example.org/examplefile.txt.

  4. The POP receives the response and redirects the request to http://www.example.org/examplefile.txt.

  5. The POP retrieves and caches the file.

  6. The POP returns the retrieved file to the user.

If other users request http://example.com/examplefile.txt, the file is served directly from the POP.

Usage notes

Before you configure 301/302 redirection for an accelerated domain name, check whether a default origin host or a specific origin host is configured for the domain name. For more information, see Configure the default origin host and Specify the origin host for each origin.

  • If no default origin host or specific origin host is configured for the domain name: When the origin server returns the HTTP 301 or 302 status code and the Location header that specifies a URL to the POP, the request is redirected to the URL that is specified by the Location header. The value of the Host header in the request is the domain name that is specified by the Location header.

  • If a default origin host is configured for the domain name: When the origin server returns the HTTP 301 or 302 status code and the Location header that specifies a URL to the POP, the value of the Host header in the request is the Host header that is configured for the accelerated domain name. If you use the domain name that is specified by the Location header as the value of the Host header in the back-to-origin request, an error occurs. If you want Alibaba Cloud CDN to support this scenario, you need to specify the origin host for each origin server.

  • The Location header that is returned from the origin server can be in the following formats:

    • Location: http://www.example.net/index.html. The POP redirects the request to the full URL specified by the Location header.

    • Location: //www.example.net/index.html. The POP redirects the request to the URL that includes the protocol that the original request uses and the value of the Location header.

    • Location: /index.html. The POP redirects the request to the URL that includes the protocol and domain name that the original request uses and the value of the Location header.

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 tree of the domain name, click Back-to-origin.

  5. On the Configurations tab, find the 301/302 Redirection section.

  6. Turn on 301/302 Redirection.

  7. Configure the parameters for 301/302 redirection. The following table describes the parameters.

    Parameter

    Description

    Maximum 301/302 Redirects

    The maximum number of times that a POP can redirect requests to the address that is specified by the Location header for each request. If the value of this parameter is exceeded, the HTTP 301 or 302 status code is returned to the user. Valid values: 1 to 5. Default value: 2.

    Note

    The maximum number of 301/302 redirects determines the number of back-to-origin requests that can be redirected to the origin server by a POP.

    Maximum number of back-to-origin requests = Maximum number of 301/302 redirects + 1. In this case, the default maximum number of back-to-origin requests is 3. Valid values: 2 to 6.

    Retain Parameters in 301/302 Redirects

    • On: In 301/302 redirects, the parameters in requests that are redirected to the origin server are retained. The request parameters are passed to the server that is specified by the Location header.

    • Off: In 301/302 redirects, the parameters in requests that are redirected to the origin server are not retained.

    Retain Request Headers in 301/302 Redirects

    • On: In 301/302 redirects, the headers in requests that are redirected to the origin server are retained. The header parameters are passed to the server that is specified by the Location header.

    • Off: In 301/302 redirects, the headers in requests that are redirected to the origin server are not retained.

  8. Click OK.

Sample configurations

Sample scenario: The resources on the origin server are relocated. You want users to request resources by using the original domain name. This way, you do not need to make user-facing changes.

Configuration: Enable 301/302 redirection for the domain name example.com. The following figure shows the configuration.回源

Expected result: Users request the file http://example.com/examplefile.txt, which is not cached on POPs. POPs redirect the requests with the original request parameters and headers retained to the origin server. The origin server receives the requests and returns the HTTP 301 or 302 status code and the URL of the file http://www.example.org/examplefile.txt to the POPs.

  1. The POPs receive the HTTP 301 or 302 status code and redirect the requests to the URL that is provided by the origin server. The Maximum 301/302 Redirects parameter is set to 2, which indicates that the requests can be redirected up to twice. If the requests fail, the HTTP 301 or 302 status code is returned to the users.

  2. The POPs retrieve the requested file, return the file to the users, and then cache the file.

  3. If other users request the file http://example.com/examplefile.txt, the file is served directly from the POPs.