All Products
Search
Document Center

Edge Security Acceleration:Forward and redirect requests

Last Updated:Feb 28, 2026

Forward and redirect HTTP requests at the edge by using an ESA edge routine. Processing requests on points of presence (POPs) instead of the origin server reduces latency, bandwidth consumption, and compute load.

How it works

image
  1. ESA schedules each user request to the optimal POP and processes it with an edge routine.

  2. The edge routine evaluates the request against custom rules.

  3. If the request matches a redirect rule, the routine returns the redirect URL directly to the client.

  4. If the request matches a forwarding rule, the routine fetches the response from the target origin server and returns it to the client.

Prerequisites

Before you begin, ensure that you have:

  • An Alibaba Cloud account with an active ESA plan, or the ability to purchase one

  • A domain name that you own (for example, example.com)

  • Access to the DNS management console for your domain (for example, Alibaba Cloud DNS)

Procedure

Step 1: Add a website

  1. In the ESA console, choose Websites and click Add Website.

  2. Enter your domain name, such as example.com, and click Next.

  3. On the Select Location and DNS Setup page, select the region in the Region for Acceleration and Protection section. Select CNAME in the DNS Setup section, and then click Next.

  4. On the Select Plan page, either purchase a plan on the New Plans tab or select an existing plan on the Purchased Plans tab. Follow the on-screen instructions to complete the purchase. #### Purchase a new plan On the New Plans tab, select a plan that suits your needs. #### Bind an existing plan If you already have a plan, select it on the Purchased Plans tab.

    image

    image

  5. Verify domain ownership:

    1. In the ESA console, click Websites, and then click your website. On the Overview page, copy the Record Value of the TXT record. image

    2. In the Alibaba Cloud DNS console, choose Public Zone in the left navigation pane. Find your domain name and click Settings.

    3. Click Add Record. Configure the following fields and click OK.

      ConfigurationExample value
      Record TypeTXT
      Hostname_esaauth
      Record Valueverify_a186***be8 (the value provided in the ESA console)
      Time to Live (TTL)10 minutes
    4. Return to the ESA console. On the Overview page of your website, click Verify.

    5. Confirm the result:

      • Verification successful -- Proceed to the next step.

      • Verification failed -- Check the DNS record settings, correct any errors, and click Verify again.

Step 2: Create the edge routine

  1. In the ESA console, choose Edge Computing > Functions & Pages in the left navigation pane.

  2. On the Functions & Pages page, click Create.

  3. Select Function Templates, choose a template provided by ESA, and then click Next.

    image

  4. Configure the basic information and click Submit.

    ParameterDescription
    Function NameA name for the function. Must start with a lowercase letter and can contain lowercase letters, digits, and hyphens (-). Cannot end with a hyphen. Length: 2-41 characters. Example: function-name. The name cannot be changed after creation.
    Description(Optional) A description of the function.
    Code PreviewA preview of the function code.

    image

Step 3: Configure the trigger

A trigger defines how an edge routine is invoked. Binding a domain name to the routine routes all requests to that domain through the edge routine.

For example, if you added the site example.com in ESA and bind function.example.com to the edge routine, requests to function.example.com, function.example.com/user, and function.example.com/login are all handled by the edge routine.

  1. In the left navigation pane, choose Edge Computing > Functions & Pages. On the Functions & Pages list page, find the target function and click Details in the Actions column.

  2. On the function details page, switch to the Domains tab and click Add Domain Name. Enter function.example.com and click OK.

  3. In the domain name binding area, find the domain name you just created. In the Actions column, click View DNS Records. Copy the CNAME value for the edge routine domain name.

    function_cname

  4. In the Alibaba Cloud DNS console, choose Public Zone in the left navigation pane. Find your domain name and click Settings.

  5. Click Add Record. Configure the following fields and click OK.

    ConfigurationDescriptionExample value
    Record TypeThe type of DNS record.CNAME
    HostnameThe subdomain prefix.function
    Record ValueThe CNAME value from the ESA console.function.example.com.a1.initmm.com
    TTLThe DNS update interval. Keep the default value.10 minutes
  6. Refresh the DNS record page and verify that the CNAME status for the edge routine domain name changes from To Be Configured to Configured.

    Note

    The CNAME record may take several minutes to propagate. If the status does not update immediately, wait and refresh again.

Step 4: Debug and publish the edge routine

  1. In the ESA console, choose Edge Computing > Functions & Pages in the left navigation pane.

  2. On the Functions & Pages page, click the function and select the Code tab.

  3. Paste the following code into the editor:

       // Scenario: Implement the following features:
       // 1. If the request path starts with "/esa/" and ends with ".html", forward the request to the official ESA website.
       // 2. If the request path starts with "/product", redirect the user request to the Alibaba Cloud product page.
       // 3. If the request does not match the rules above, redirect it to the Alibaba Cloud home page.
    
    
       // Define a regular expression: The path starts with "/esa/" and ends with ".html".
       const productPageRegex = /^\/esa\/.*\.html$/;
    
       function handleRequest(request) {
         const url = new URL(request.url);
         const pathName = url.pathname;
    
          // If the path starts with "/esa/" and ends with ".html", forward the request to the ESA product page.
         if(productPageRegex.test(pathName)) {
           const newUrl = 'https://www.aliyun.com/product/esa';
           return fetch(new Request(newUrl, request));
         }
    
         // If the path starts with "/product", redirect to the Alibaba Cloud product page.
         if (pathName.startsWith('/product')) {
           const newUrl = 'https://www.aliyun.com/product/list';
           return Response.redirect(newUrl, 301);
         }
    
         // If the request does not match the rules above, redirect to the Alibaba Cloud home page.
         const newUrl = 'https://www.aliyun.com/';
         return Response.redirect(newUrl, 301);
       }
    
       export default {
         fetch(request) {
           return handleRequest(request);
         }
       }
  4. Click Save. In the debugging area to the right of the code editor, construct an HTTP request by specifying the method, request header, and request body. Click Request to send the request. The console returns the response after the routine processes the request.

  5. After debugging, click Generate Version.

  6. Click the Deploy tab and choose Release. Select the version you generated and click Release. Choose to publish to either the staging environment or the Production Environment.

    Note

    Publish to the staging environment first. After you verify that the routine works correctly, publish it to the Production Environment.

Verify the result

Important

In the following examples, replace function.example.com with the domain name you bound in Step 3.

Verify forwarding

Open your browser and go to http://function.example.com/esa/info.html. The browser displays the Alibaba Cloud ESA home page while the address bar remains unchanged. This confirms that the forwarding rule works correctly.

Verify redirection

  1. Open your browser and go to http://function.example.com/product. The browser redirects to https://www.aliyun.com/product/list, and the address bar updates to the new URL.

  2. Open the browser developer tools and inspect the request for http://function.example.com/product. Confirm the following: esa_redirect

    • Status Code: 301 Moved Permanently

    • Location header: https://www.aliyun.com/product/list

Clean up resources

After testing, delete the resources you created to avoid unnecessary charges.

  1. Delete the website from ESA. In the ESA console, click Websites in the left navigation pane. Find your website and click Delete in the Actions column.

  2. Delete the edge routine. In the ESA console, choose Edge Computing > Functions & Pages in the left navigation pane. Click the target function. On the Basic Information page, click Delete in the Actions column.

  3. Delete the DNS record. In the Alibaba Cloud DNS console, choose Public Zone. Find your domain name and click Settings. Locate the host record you configured in Step 3 and click Delete.