Edgio

Redirects

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';
2
3export default new Router()
4 .use(edgioRoutes)
5
6 .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';
4
5 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';
2
3export default new Router()
4 .use(edgioRoutes)
5
6 .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 code
3 const newUrl = `${request.url}/${country}`; // Change the redirect URL to your choice
4
5 // Redirect the request if the client is in the specified country
6 if (context.geo.country === country) {
7 return Response.redirect(newUrl, 302);
8 }
9
10 // Return the original request if the client is not in the specified country
11 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 redirects
3 {
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 },
13
14 // dynamic redirects by pattern
15 {
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';
3
4export default new Router()
5 .use(edgioRoutes)
6
7 // Match the source (`from`) path of each redirect
8 .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';
2
3const NOT_FOUND = new Response('Not Found', {status: 404});
4const BAD_REQUEST = new Response('Bad Request', {status: 400});
5
6export async function handleHttpRequest(request, context) {
7 const url = new URL(request.url);
8
9 // Find the redirect that matches the request path
10 const redirect = redirects.find((r) => url.pathname.match(r.from));
11
12 // If there is no matching redirect, return a 404 response
13 if (!redirect) {
14 return NOT_FOUND;
15 }
16
17 // If there is a matching static redirect, return a redirect response
18 if (redirect.to) {
19 return Response.redirect(redirect.to, redirect.status);
20 }
21
22 // For a matching dynamic redirect, forward the path to the origin and redirect based on the response
23 const response = await fetch(request.url, {
24 edgio: {origin: 'web'},
25 });
26
27 // Assume the response is JSON with a `redirect` and `status` property
28 try {
29 const {redirect, status} = await response.json();
30
31 // If the response contains a redirect, return a redirect response
32 if (redirect) {
33 return Response.redirect(redirect.to, redirect.status);
34 }
35
36 // If the response does not contain a redirect, return a 404 response
37 return NOT_FOUND;
38 } catch (e) {
39 // If the response fails to parse as JSON, return a 400 response
40 return BAD_REQUEST;
41 }
42}