Easily redirect one entire domain to another with a serverless Cloudflare Worker.

Overview

Domain Redirecting with Cloudflare Workers

Easily redirect one entire domain to another with a serverless Cloudflare Worker.

All paths and other data are kept intact. The redirect served is 302 Found (temporary redirect) to prevent cache headache if you ever change your mind.

The aim is to replace lots of Page Rules like this:

And instead use a simple map in domainMaps.json:

{
    "erisa.moe": "erisa.uk",
    "erisa.wales": "erisa.uk",
    "erisa.cymru": "erisa.uk",
    "link.erisa.moe": "erisa.link",
    "cloud.erisa.moe": "erisa.cloud"
}

Note: Cloudflare Workers is a billed product with a free tier available. Page Rules are preferred if cost savings are your priority. This Worker is used to keep domain mapping centralised and to free up Page Rules for other purposes. This is not always what you want.

Code is provided for reference and curiosity purposes, project is only intended to be used personally by the author.
But if you like it, you can use it!

Usage

Required:

  • Cloudflare DNS
  • Wrangler (npm i -g wrangler or cargo install wrangler)

Deploy:

  1. Clone the repository
  2. Edit domainMaps.json with source/target pairs of domains to redirect.
  3. wrangler publish

After deploying the worker, ensure that the source domain has a valid DNS record (If not, create an AAAA pointing to 100::) and then head to the Workers tab. Configure example.domain.com/* to use your worker:

Domains that are mapped to the Worker but not listed in domainMaps.json will return a 404 error:

That's all! It's quite simple.

You might also like...

slash-create with Cloudflare Workers template

/create with Cloudflare Workers A slash-create template, using Cloudflare Workers. Getting Started Cloning the repo You can either use degit to locall

Jan 3, 2023

Airtable + Cloudflare Workers URL Shortener 🌤

Airtable + Cloudflare Workers URL Shortener 🌤

tableflare Airtable + Cloudflare Workers URL Shortener 🌤 Quick Start Airtable Generate your Airtable API key from your account dashboard: https://air

Oct 16, 2022

Build your Cloudflare Workers with esbuild.

build-worker Bundle your Cloudflare Worker with esbuild instead of webpack. (It's ridiculously faster!) Wrangler v1 uses webpack. Wrangler v2 is using

Oct 24, 2022

Store and Deliver images with R2 backend Cloudflare Workers.

Store and Deliver images with R2 backend Cloudflare Workers.

r2-image-worker Store and Deliver images with Cloudflare R2 backend Cloudflare Workers. Synopsis Deploy r2-image-worker to Cloudflare Make a base64 st

Jan 3, 2023

Backend serverless que simula o sistema de votação do BBB

Backend serverless que simula o sistema de votação do BBB

SSS - Super Serverless Sample Esse é um projeto de exemplo de arquitetura serverless usando como inspiração a votação para eliminação do paredão do BB

Aug 23, 2022

Serverless URL Shortener made with Next.js + Redis.

Serverless URL Shortener made with Next.js + Redis.

linki: a place for your links linki is a url shortener made with next.js and redis! built with simplicity in mind, it's all in one page. deploy your o

Sep 15, 2022

Hello Jobs is a one-stop solution for all job seekers. In future, this could also serve as a platform for recruiters to hire potential candidates.

Hello Jobs Hello Jobs is a one-stop solution for all job seekers. In future, this could also serve as a platform for recruiters to hire potential cand

Dec 26, 2022

Redirect requests of current origin to another domain with Service Worker.

Service Worker to Redirect Origin This is a tool for your static website which could intercept all GET requests of the origin domain and redirect them

Aug 28, 2022

Marketing-redirect - Redirect your customers from your marketing page to the application they're paying for

Marketing Redirect Do you have a marketing page and an app with two separate, bu

Feb 11, 2022

Cloudflare Worker that will allow you to progressively migrate files from an S3-compatible object store to Cloudflare R2.

A Cloudflare Worker for Progressive S3 to R2 Blog Post: https://kian.org.uk/progressive-s3-to-cloudflare-r2-migration-using-workers/ This is a Cloudfl

Dec 30, 2022

Connect to a Postgres database from a Cloudflare Worker, using Cloudflare Tunnel

Cloudflare Workers Postgres Client This is an experimental module. Heavily based on cloudflare/worker-template-postgres, but cleaned up and bundled in

Dec 22, 2022

Build your entire project with one command 🚀

Build your entire project with one command 🚀

Dev Template dt --template mvc No more installing and configuring all basic dependencies manually every time you start a new project. the dev template

Nov 8, 2022

This is another Express + TypeScript + DDD (Domain Driven Design patterns) + IoC/DI (Inversion of control and Dependency injection) + Primsa ORM + API REST boilerplate.

Express-TS-DDD REST API This is another Express + TypeScript + DDD (Domain Driven Design patterns) + IoC/DI (Inversion of control and Dependency injec

Nov 3, 2022

Use Cloudflare Pages Functions as a reverse proxy with custom domain support.

Use Cloudflare Pages Functions as a reverse proxy with custom domain support.

cf-page-func-proxy Use Cloudflare Pages Functions as a reverse proxy with custom domain support. Getting Start 1.下载或是Fork本仓库 2.修改_worker.js中的url.hostn

Dec 23, 2022

serverless plugin to setup custom domain for lambdas deployed with function url.

serverless-aws-function-url-custom-domain Automatically creates AWS CloudFront distribution and Route 53 records to AWS Lambda with Function URL (no a

Nov 27, 2022

Get-A-Room example application using Domain Driven Design and Clean Architecture. Written in TypeScript and deployed to AWS with a serverless stack.

Get-A-Room example application using Domain Driven Design and Clean Architecture. Written in TypeScript and deployed to AWS with a serverless stack.

Domain Driven Microservices on AWS in Practice This project provides a Domain Driven Design & Clean Architecture-informed, multi-service event-driven

Dec 31, 2022

Yet-Another-Relog-Mod - Just another relog mod. Call it YARM!

Yet-Another-Relog-Mod - Just another relog mod. Call it YARM!

Yet Another Relog Mod A relog mod with a name so long, you can just call it YARM for short. Features An aesthetic relog list design that follows my "p

Oct 19, 2022

A template for WebSockets powered Cloudflare Worker project using graphql-ws

🚡 graphql-ws on Cloudflare Workers A template for WebSockets powered Cloudflare Worker project using graphql-ws. The worker serves the following rout

Dec 18, 2022
Comments
  • URL redirect

    URL redirect

    Hi Erisa.

    Is it possible to have your domain-redirect worker support URL redirect as well?

    I.e. blog.domain.com -> domain.com/blog/

    I have tried just to just put a URL in the domainMaps.json but when published all blog.domain.com visitors get a 'Worker Error Exception' page in the browser and an "Invalid hostname when stringifying URL" error pops up in the worker stream log.

    The Cloudflare free-tier only permits 3-page rules and for my use case, I have both domain and URL redirection rules configured. URL-redirection would let me (and a whole lot of other free-tier users) kill two birds with one stone.

    enhancement 
    opened by KimTholstorf 2
  • Set an account_id

    Set an account_id

    (building stuff... snip'ed)
    ✨  Built successfully, built project size is 677 bytes.
    👀  You have multiple accounts.
    🕵️  You can copy your account_id below
    +---------------------+----------------------------------+
    | Account Name        | Account ID                       |
    +---------------------+----------------------------------+
    | [REDACTED] | [REDACTED] |
    +---------------------+----------------------------------+
    | Me        | [REDACTED] |
    +---------------------+----------------------------------+
    Error: field `account_id` is required
    

    account_id is required when you have more than one account in your login profile. Add them as optional flags so someone who might use it don't need to figure out what to do (well, it's pretty trivial but saving time is always good...)

    opened by revi 1
Owner
Erisa A
22 y.o. female weirdo with a passion for making a mess of things.
Erisa A
Cloudflare Worker that will allow you to progressively migrate files from an S3-compatible object store to Cloudflare R2.

A Cloudflare Worker for Progressive S3 to R2 Blog Post: https://kian.org.uk/progressive-s3-to-cloudflare-r2-migration-using-workers/ This is a Cloudfl

Kian 29 Dec 30, 2022
Send emails using Cloudflare Worker, for free.

Email API for Proselog. Not intended for use outside of Proselog, but it should work with any worker, without any configuration. import { sendEmail }

Proselog 65 Nov 7, 2022
Cloudflare Worker to make a R2 Bucket public!

r2-public-worker A Cloudflare Worker to make your R2 bucket public! Minimum Requirements Cloudflare Account wrangler >= 2.0.2 Note: Ensure you are usi

Cole Mackenzie 20 Sep 19, 2022
Using Cloudflare worker to generate host list from firebog to keep updated.

AdGuardCloudflareHostGenerator Use a cloudflare worker to generate a up to date list from FireBog's ticked list found at https://v.firebog.net/hosts/l

Jake Steele 14 Nov 30, 2022
Use Cloudflare Workers Cron Triggers to keep your Hetzner Cloud Firewall allowing the latest list of Cloudflare IPs, or any other lists!

Hetzner Cloud Firewall automation with Cloudflare Workers Heavily inspired by xopez/Hetzner-Cloud-Firewall-API-examples, this repository holds a Cloud

Erisa A 9 Dec 17, 2022
A tiny utility to asynchronously drive a namespace exposed through a Worker.

proxied-worker Social Media Photo by Ricardo Gomez Angel on Unsplash A tiny utility to asynchronously drive a namespace exposed through a Shared/Servi

Andrea Giammarchi 43 Dec 8, 2022
A tool library for handling window && iframe && worker communication based on the JSON RPC specification

rpc-shooter A tool library for handling window && iframe && worker communication based on the JSON RPC specification 一个基于 JSON-RPC 规范用于处理 window && if

臼犀 89 Dec 20, 2022
⚙️ Offline-capable Astro apps via SWSR (Service Worker Side Rendering)

Astro-service-worker ⚙️ Offline-capable Astro apps via SWSR (Service Worker Side Rendering) astro-service-worker will take your Astro SSR project, and

Pascal Schilp 41 Dec 4, 2022
Type-safe and Promisified API for Web Worker and Iframe

?? You can help the author become a full-time open-source maintainer by sponsoring him on GitHub. typed-worker Install npm i typed-worker Usage Create

EGOIST 189 Dec 31, 2022
Yet another concurrent priority task queue, yay!

YQueue Yet another concurrent priority task queue, yay! Install npm install yqueue Features Concurrency control Prioritized tasks Error handling for b

null 6 Apr 4, 2022