All Products
Search
Document Center

Edge Security Acceleration:302 redirect

Last Updated:Apr 29, 2025

The 302 redirect through Edge Routine enables temporary redirect from one URL to another. Use it during website resource migration, upgrades or maintenance to enhance user experience and security.

When to use it

Use the Edge Routine to generate a 302 redirect response and ESA point of presence (POP) automatically redirect the client request to the target address to fetch the content. This improves response speed by reducing redirect interactions.

Note

If your business requires the origin server to respond, use the 302 redirect follow feature.

Sample code

  • Expected effect: When a client initiates a request, the Edge Routine's resposnse contains a 302 status code, and all incoming requests will be redirected to the specified URL.

  • Language type: Javascript

  • Example:

    /**
     * Creates a custom function named handleRequest to process requests.
     * @param {Request} request - The request object initiated by the client
     * @returns {Response} - Returns a redirect response
     */
    function handleRequest(request) {
      // Defines a new URL, and all requests will be redirected to this address.
      const newUrl = 'https://example.com'; // The target address
      
      // Returns a 302 redirect response
      // Response.redirect is a convenient method for creating a redirect response with a specified status code.
      // The first parameter is the target URL and the second parameter is the status code (302 indicates temporary redirect).
      return Response.redirect(newUrl, 302);
    }
    
    /**
     * Defines the default fetch function, which is the entry point for handling all incoming requests
     * @param {Request} request - The request object initiated by the client
     * @returns {Response} - Returns the processed response
     */
    export default {
      fetch(request) {
        // Calls the handleRequest function to process the request
        return handleRequest(request);
      }
    }

Deployment effect

  • Client request

    GET /some-path HTTP/1.1
    Host: your-deployed-service.com
  • Server response

    The client will automatically follow the redirect and initiate a new request to the target URL https://example.com.

    HTTP/1.1 302 Found
    Location: https://example.com