Redirects can be used to redirect a request to a different URL. This can be useful for redirecting users to a different page, or for redirecting requests to a different origin.
Basic Redirect
A basic redirect can be performed by returning a
Response
object with a Location
header set to the URL to redirect to.Router Configuration
JavaScriptroutes.js
1import {Router, edgioRoutes} from '@edgio/core';23export default new Router()4 .use(edgioRoutes)56 .match('/some/path', {7 edge_function: './edge-functions/main.js',8 });
Edge Function
JavaScriptedge-functions/main.js
1export async function handleHttpRequest(request, context) {2 const url = new URL(request.url);3 url.pathname = '/some/other/path';45 return Response.redirect(url.toString(), 302);6}
Geolocation Redirect
Redirects can be performed based on the client’s geolocation. This can be useful for redirecting users to a different page based on their country or region.
Router Configuration
JavaScriptroutes.js
1import {Router, edgioRoutes} from '@edgio/core';23export default new Router()4 .use(edgioRoutes)56 .match('/some/path', {7 edge_function: './edge-functions/main.js',8 });
Edge Function
JavaScriptedge-functions/main.js
1export async function handleHttpRequest(request, context) {2 const country = 'DE'; // Choose a country code3 const newUrl = `${request.url}/${country}`; // Change the redirect URL to your choice45 // Redirect the request if the client is in the specified country6 if (context.geo.country === country) {7 return Response.redirect(newUrl, 302);8 }910 // Return the original request if the client is not in the specified country11 return fetch(request.url, {12 edgio: {origin: 'echo'},13 });14}
Bulk Redirects
Bulk redirects can be performed by specifying a list of redirects in both the router configuration and the edge function. This can be accomplished for a large number of static redirects such as a JSON file containing a list of redirects, or an API call to a third-party service that provides a list of redirects.
Static Redirects
JavaScriptredirects.js
1export default [2 // static redirects3 {4 from: '/some/path',5 to: '/some/other/path',6 status: 302,7 },8 {9 from: '/api/v1/users',10 to: '/api/v2/users',11 status: 302,12 },1314 // dynamic redirects by pattern15 {16 from: /^\/products\/(.+)$/,17 to: null,18 status: 302,19 },20];
Router Configuration
JavaScriptroutes.js
1import {Router, edgioRoutes} from '@edgio/core';2import redirects from './redirects';34export default new Router()5 .use(edgioRoutes)67 // Match the source (`from`) path of each redirect8 .if(9 {path: redirects.map((r) => r.from)},10 {11 edge_function: './edge-functions/main.js',12 }13 );
Edge Function
JavaScriptedge-functions/main.js
1import {redirects} from '../redirects';23const NOT_FOUND = new Response('Not Found', {status: 404});4const BAD_REQUEST = new Response('Bad Request', {status: 400});56export async function handleHttpRequest(request, context) {7 const url = new URL(request.url);89 // Find the redirect that matches the request path10 const redirect = redirects.find((r) => url.pathname.match(r.from));1112 // If there is no matching redirect, return a 404 response13 if (!redirect) {14 return NOT_FOUND;15 }1617 // If there is a matching static redirect, return a redirect response18 if (redirect.to) {19 return Response.redirect(redirect.to, redirect.status);20 }2122 // For a matching dynamic redirect, forward the path to the origin and redirect based on the response23 const response = await fetch(request.url, {24 edgio: {origin: 'web'},25 });2627 // Assume the response is JSON with a `redirect` and `status` property28 try {29 const {redirect, status} = await response.json();3031 // If the response contains a redirect, return a redirect response32 if (redirect) {33 return Response.redirect(redirect.to, redirect.status);34 }3536 // If the response does not contain a redirect, return a 404 response37 return NOT_FOUND;38 } catch (e) {39 // If the response fails to parse as JSON, return a 400 response40 return BAD_REQUEST;41 }42}