r/CloudFlare: Using Workers redirect one specific webpage (URL) to an outside website

r/CloudFlare: Using Workers redirect one specific webpage (URL) to an outside website
This article archives a conversation, which took place in a subreddit post (original source linked below) and to which I contributed a solution or answer (with the u/MasterofSynapse handle), in a Q&A format.

Original Reddit post: https://www.reddit.com/r/CloudFlare/comments/zfgm8i/using_workers_redirect_one_specific_webpage_url/


I followed the instructions to try to redirect all visits to a the URL www.example.com/easytoremember to go to www.zoom.us/randomwebinarID83989u3sj3j23sj0j2jssj2s.

Here are the instructions I tried to follow:


I added the code, pressed Save and deploy. Then I pressed send to test it. I get a 404 error. Here is my code. What am I doing wrong?

const redirects = new Map([
  ["cloudflare-shortener", "https://www.example.com/easytoremember"],
  ["neil-pattel-copied-my-design", "https://www.zoom.us/randomwebinarID83989u3sj3j23sj0j2jssj2s"],
addEventListener('fetch', event => {
async function handleRedirect(request) {
  let pathname = new URL(request.url).pathname.replace("/", "");
  let location = redirects.get(pathname);
  return location 
    ? Response.redirect(location, 301) 
    : new Response("Not Found", {status: 404})


Is there a reason you want to do this via Workers? Do you want to be able to feed it via an API or will you manually enter each redirect?

In that case you can deploy a Cloudflare Pages project and use the _redirects file in there, here are the docs to it: https://developers.cloudflare.com/pages/platform/redirects/

Alternatively, you could use Bulk Redirects. You can find a guide for that here: https://epsilonsynapse.com/tech-salvation/definitive-guide-on-cloudflare-redirects/#bulk-redirects

Comment 1 on Answer

I looked into Pages redirect at Cloudflare but it won't work for me because my website is not hosted at Cloudflare. We use Cloudflare for firewall/DNS stuff. I should have specified that. Based on this fact, is there any solution at Cloudflare to allow me to redirect one single page to an outside website?

My response to comment 1

The second way with the Bulk Redirects will still work for you. You would only have to enable the Proxy on the DNS record you want the redirect to be active on and configure it like the blog post tells you to.

Comment 1.1 on Answer

I read the guide and followed the steps for bulk redirect. Here is my follow up question:

  1. When setting up bulk directs, do I need to follow the instructions at the top about adding A and AAA DNS records, as discussed at the top of the guide? (Because I have another A record that already points to an IP number.) I'm not sure if the bulk redirect section stands alone and nothing of what went before is needed.

My response to comment 1.1

The DNS paragraph has to be followed always. And you can have multiple A records, just not more than one with the same name.

Comment 1.2 on Answer

Thanks for trying to clear this up for me. I currently have the following (my real data obscured):

Type: A | Name: www | Content: 555.5.2.1 | Proxy status: Proxied | TTL: Auto
Type: A | Name: mydomain.org | Content: 555.5.2.1 | Proxy status: Proxied | TTL: Auto
The instructions require me to add this:

Type: A | Name: www | Content: | Proxy status: Proxied | TTL: Auto Type: A | Name: @ | Content: | Proxy status: Proxied | TTL: Auto

But you wrote I cannot use the same name, and the www entry would have the same name. So, I'm confused. How can I follow these instructions without breaking the DNS rules?

My response to comment 1.2

The DNS instructions have to be applied if no proxied DNS records exist in the respective zone yet. In your case, since you already proxy on www and @, you can ignore the DNS instructions and it will work.