How to Create a Cloudflare Worker for Bulk Redirects During Site Migrations

Last Updated: Thursday October 5, 2023

In the realm of site migrations, there are times when you'll face a roadblock. Perhaps you can't access the hosting of the source site, or the CMS of the source site doesn't cater to bulk redirects, or you simple don't want to continuing paying for hosting to implement redirects and prefer to implement these redirects directly at the DNS level.

When the architecture of your site undergoes a transformation, especially when you're dealing with a vast number of pages, things can get tricky. If you've thought of using Cloudflare's built-in "bulk redirect" tool, you'll quickly find its limitations, with only 20 redirects for Free Users, 500 for Pro Users, and 10,000 for Enterprise Users. Details on their availability can be found here. But what if you have a website with more than 10,000 pages and each of them needs individual mapping? Or what if you're not quite ready to invest in an upgrade for just this feature?

Enter the Cloudflare worker, a potent tool that can handle this challenges:

The Code Behind the Magic

Below is a simple yet effective code for the Cloudflare worker that can manage the redirects:


export default {
async fetch(request) {

const redirectMap = new Map([
["/about-us", "https://destinationurl.com/about-us"],
["/about-us/team", "https://destinationurl.com/about-us/team"],
["/about-us/csr", "https://destinationurl.com/about-us/csr"],
]);

const requestURL = new URL(request.url);
const path = requestURL.pathname;
const location = redirectMap.get(path);

if (location) {
return Response.redirect(location, 301);
}
// If request not in map, return the original request
return fetch(request);
},
};

This code is a nifty tool that uses a map structure to dictate the redirects. All you need to do is list down the old and new URLs in pairs, and the worker will seamlessly guide your users to the right pages.

Wrapping Up

Site migrations are complex, and with the constant evolution of digital infrastructure, you'll need powerful tools in your toolkit. This Cloudflare worker is an example of how you can adapt and navigate efficiently through the challenges, ensuring smooth transitions for your users and maintaining the SEO value of your older URLs.

Remember, the key is in details, preparation, and the right tools. Happy migrating!

Knowledge Base Articles

Written by Shahin Fard

A digital pioneer and the brains behind Bravr Ltd,
With over two decades of expertise in the online world
A proud dad of two kids and a keen car enthusiast.

Contact us


    SEOPaidWeb Design/DevHostingTrainingOther/Not Sure

    Please note:

    • This form stores your inputted data and your information about your device. This enables us to respond to your message and combat automated submissions (spam).

    • We will never share your personal information with anyone outside of Bravr Ltd, unless you permit us to.

    • We do not consent to being contacted by companies or individuals trying to sell us services.

    • We track IP addresses, report and ban IP’s of those who choose to spam us.