Improve URL Redirects with EdgeWorkers and EdgeKV
URL redirects are a way to send users to a different URL from the one initially requested. These redirects are common to web applications, but can be challenging to effectively manage and orchestrate at scale.
Akamai EdgeWorkers and Akamai EdgeKV make URL redirects at the Akamai Intelligent Edge easier than ever. With EdgeWorkers, developers can create and deploy microservices on the largest distributed serverless network. Meanwhile, EdgeKV helps you build data-driven EdgeWorkers applications that require fast, frequent reads and infrequent writes.
Read on to learn why pairing EdgeWorkers and EdgeKV can be a powerful solution for effectively handling redirects at the Akamai Intelligent Edge, and when to use EdgeWorkers for redirects.
Unpacking the challenges of managing redirects
URL redirects typically entail sending a user who requests page A to page B. Many tools exist to manage simple URL redirects, such as Akamai Edge Redirector.
These tools are a great way for nontechnical users to support new campaigns by implementing simple URL redirects with a point-and-click interface. However, other redirect types necessitate more complicated processes than simply redirecting traffic. You often need strong business logic to make intelligent routing decisions for site migrations, promotional campaigns, or loyalty programs.
There are also search engine optimization (SEO) best practices that are critical to observe and uphold when creating new redirects. Unfortunately, existing tools often fail to solve these complex redirect cases. This article offers a solution to a URL redirect problem for a site migration that requires logic that exceeds the limits of traditional redirect tools.
A smarter way of redirecting URLs
Akamai EdgeWorkers can be used to both deploy JavaScript code to the Akamai Intelligent Edge and to execute redirects based on data stored and read from an EdgeKV database.
For this example, we’ll assume the old site is using a product SKU within a relevant URL. However, the newly revamped site uses a product tag to improve SEO:
Old URL: www.example.com/sku/<product_sku>/*
New URL: www.example.com/products/<product_tag>/*
For example, the product tag “483D5F” correlates and redirects to “straight-fit denim jeans”:
Old URL: www.example.com/sku/483D5F/reviews
New URL: www.example.com/products/straight-fit-denim-jeans/reviews
To incorporate this approach to URL redirecting with EdgeWorkers and EdgeKV, we can use a regular expression to match the incoming URL as seen by the edge server. We then look up the old SKU and read the new SKU tag data from EdgeKV. Finally, we dynamically generate the new redirect rule and send it to the intended browser.
This workflow would be extremely challenging with traditional redirect tools, and would likely require writing individual redirects for each SKU. But with EdgeWorkers and EdgeKV, we’re able to get a workable solution online with fewer than 15 lines of JavaScript code.
In doing so, we also execute logic at the Akamai Intelligent Edge to make this millisecond-quick redirect decision. By getting the user to their desired product faster and more efficiently than by implementing this logic at the origin, we improve the end-to-end user experience — a win all around.
URL redirect code
Here's the final code:
import { EdgeKV } from './edgekv.js';
// Initialize EdgeKV library
const edgeKv_products = new EdgeKV({namespace: "default", group: "products"});
// Regex to match URL
const URL_REGEX = /\/sku\/(?<sku>[a-zA-Z0-9]+)\/(?<suffix>.*)/;
export async function onClientRequest(request) {
let regexMatch = request.url.match(URL_REGEX);
// If regex matches URL, generate redirect
if (regexMatch) {
let sku = regexMatch.groups.sku;
// Load product data from EdgeKV
let productData = await edgeKv_products.getJson({ item: sku});
if (productData) {
//Construct new URL, using product tag stored in EdgeKV
let newUrl = `/products/${productData.tag}/${regexMatch.groups.suffix}`
//Respond with 301 redirect
request.respondWith (301, {location:[newUrl]}, "");
}
}
}
Example EdgeKV data:
{
"sku": "483D5F",
"tag": "straight-fit-denim-jeans"
}
URL redirect code, explained
Step 1
import { EdgeKV } from './edgekv.js';
First, we import the JavaScript module that will be used. For reference, you can see all the available built-in modules supported by EdgeWorkers.
Note the imported edgekv.js file. This is a utility library published by Akamai that simplifies the process of working with EdgeKV. Read the full documentation in the helper library.
Step 2
// Initialize EdgeKV library
const edgeKv_abpath = new EdgeKV({namespace: "default", group: "ab-data"});
Next, we initialize the EdgeKV database. For testing purposes, this example uses the default namespace. Read more about initializing EdgeKV.
Steps 3 through 6 happen during the client request stage. See more about the EdgeWorkers event model.
Step 3
let regexMatch = request.url.match(URL_REGEX);
if (regexMatch) {
. . .
}
Using a regex match condition on the incoming request, we can limit redirect logic execution to only requests matching the SKU pattern. Learn more about the request object and its supported properties and methods.
Step 4
let productData = await edgeKv_products.getJson({ item: sku});
This code reads the URL redirect configuration data from our EdgeKV database using the SKU as the key in the key-value pair. In this example, the data is read as JSON, though it could also be read as a text string. Read the documentation on both getter functions.
Step 5
let newUrl = `/products/${productData.tag}/${regexMatch.groups.suffix}`
After reading the data from EdgeKV, we can dynamically construct the new redirect URL. This functionality is impossible without the power and flexibility afforded by EdgeWorkers and EdgeKV from Akamai.
Step 6
request.respondWith (301, {location:[newUrl]}, "");
This final line of code responds with the newly constructed redirect URL. In this example, we are using a 301 HTTP response code, but you could use a similar 302 or 307 HTTP status code. Read the documentation on the respondWith method.
With just a few simple lines of code, EdgeKV and EdgeWorkers can create logic that effectively determines when to apply a URL redirect based on the request characteristics, then dynamically generate a new URL using configuration data and serve the redirect containing the new URL.
Faster URL redirects start with EdgeKV and EdgeWorkers
If you’re ready to offer users better web browsing experiences through improved URL redirection, read the EdgeKV and EdgeWorkers documentation to learn more about what both products can do for your business. Once you’re up and running with EdgeKV and EdgeWorkers, follow the outlined steps to package and run each code sample discussed above. To load the A/B test scenario into EdgeKV, follow a required additional step.