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!
Required:
- Cloudflare DNS
- Wrangler (
npm i -g wrangler
orcargo install wrangler
)
Deploy:
- Clone the repository
- Edit
domainMaps.json
with source/target pairs of domains to redirect. 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.