Code Monkey home page Code Monkey logo

domain-redirect's Introduction

Deprection notice

This project has been deprecated and archived.

I recommend you use Cloudflare's native Bulk Redirects or Dynamic Redirects features to redirect domains, rather than Page Rules.

You may use or fork this project if you desire, but it is not supported by me and will not recieve any updates.

The original README is below.

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.

domain-redirect's People

Contributors

erisa avatar revi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

reviforks akumax1

domain-redirect's Issues

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.