Introducing Edgio Applications v7Find out what's new.
Edgio
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
1import {URL} from 'whatwg-url';
2
3export async function handleHttpRequest(request, context) {
4 const url = new URL(request.url);
5 url.pathname = '/some/other/path';
6
7 return Response.redirect(url.toString(), 302);
8}

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