Set up a (sub)-domain proxy for Github Pages.
This worker creates a proxy that works like this:
domain.com
→username.github.io
domain.com/project/
→username.github.io/project/
project.domain.com
→username.github.io/project/
ℹ️ This is NOT a URL redirection. It really proxies and hides the original username.github.io
.
You'll need a Cloudflare Workers account with:
- A workers domain set up
- Some GitHub-Pages hosted web pages
Prepare the following secrets:
CF_API_TOKEN
: Cloudflare API token withEdit Cloudflare Workers
permissionsCF_ACCOUNT_ID
: Cloudflare account ID
- Clone/fork this repository.
- Edit
wrangler.toml
. Change theROOT_GH
variable. - Install dependencies by running
npm i
. - Test it locally by running
npm start
. - Set up the secrets mentioned above if using GitHub Actions to deploy.
- Publish by running
npm run publish
.
Go to your Cloudflare dashboard, click on a web site, click to Workers
, press Add route
.
Add your domain to the Route
field. Two options:
domain.com/*
: if you only want to proxy the sub-directories*domain.com/*
: if you want to proxy the sub-domains and sub-directories
For the wildcard subdomains to work, click to DNS
, and press Add record
.
Choose CNAME
, use *
as Name
and set the Target
to your domain.
Two extra headers are set:
x-original-url
: The original URL that's requestedx-destination-url
: The URL that's proxied
They are useful for debugging purposes.