All Products
Search
Document Center

ApsaraVideo VOD:Configure 301/302 redirection

Last Updated:Aug 21, 2023

The 301/302 redirection feature supports HTTP 301 and 302 status codes that are returned by the origin server. After you configure 301/302 redirection, Alibaba Cloud CDN points of presence (POPs) process the HTTP 301 or 302 status code that is returned by your 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 fails to access the resource. In most cases, the server adds the Location header to the response header to address 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 the origin server and receives the HTTP 301 or 302 status code from the origin server, the POP processes the status code. The POP redirects 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. How it works
  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 to the address 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 send requests to http://example.com/examplefile.txt, the file is returned to the users because the file is already cached on POPs.

Usage notes

Before you configure 301/302 redirection for an accelerated domain name, check whether an origin host is configured for the domain name. For more information, see Configure an origin host.

  • If an origin host is not configured: 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 an origin host is configured: When the origin server returns the HTTP 301 or 302 status code and the Location header that specifies a URL to the CDN POP, the value of the Host header in the request is the Host header that is configured for the accelerated domain name. By default, the value of the Host header is the accelerated domain name. If you require that the value of the Host header in the back-to-origin request must be the domain name specified by the Location header, an error occurs. However, if you have such requirement, submit a ticket. For more information about how to submit a ticket, see Contact us.

Procedure

  1. Log on to the ApsaraVideo VOD console.

  2. In the left-side navigation pane, choose Configuration Management > CDN Configuration > Domain Names. The Domain Names page appears.

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

  5. On the Configure tab, turn on 301/302 Redirection in the 301/302 Redirection section and configure the parameters.

    ParameterDescription
    Maximum 301/302 RedirectsThe maximum number of times that a POP can redirect 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.CDN 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, 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, 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, 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, headers in requests that are redirected to the origin server are not retained.

  6. Click OK.

Configuration examples

Sample scenario: The origin server relocated to another domain name. You want users to visit the origin server through the original domain name so that you do not need to make user-facing changes.

Configuration: Enable 301/302 redirection for the domain name example.com. The following content describes the configuration.

  • Maximum Number of 301/302 Redirects: 2

  • Retain Parameters in 301/302 Redirects: No

  • Retain Request Headers in 301/302 Redirects: No

Expected result: Users request the file http://example.com/examplefile.txt, which is not cached on POPs. POPs redirect the requests to the origin server with the original request parameters and headers retained. 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 to the URL that is provided by the origin server. Maximum 301/302 Redirects 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 it to the users, and cache it.
  3. Other users can retrieve the file http://example.com/examplefile.txt from the POPs.