JavaScript Examples - Redirect All Requests
Redirect every incoming request to a single destination URL directly from Azion’s global network. This is helpful during maintenance windows or downtime, when you want to point all visitors to a status page or an alternate site without changing your origin.
const destinationURL = "https://www.azion.com/en/" const statusCode = 301
async function handleRequest(request) { return Response.redirect(destinationURL, statusCode) }
addEventListener("fetch", async event => { event.respondWith(handleRequest(event.request)) })How it works
The function registers a handler for the fetch event with addEventListener. Inside handleRequest, it calls Response.redirect(destinationURL, statusCode) to build a redirect response that points the browser to the configured URL, using the 301 status code to signal a permanent move. Because destinationURL and statusCode are constants, every request receives the same redirect regardless of the original path. event.respondWith() then returns that response, so visitors are redirected without the request ever reaching your origin.