Redirecting a domain using Cloudflare Pages
I wanted to redirect https://global-power-plants.datasettes.com/ to https://datasette.io/ - I decided to spin up a Cloudflare Pages site to do the work.
I created a GitHub repo at https://github.com/simonw/cloudflare-redirects which I plan to use for other such sites, and created these files:
global-power-plants.datasettes.com/index.html
<!DOCTYPE html><html><head> <title>Redirecting...</title> <meta http-equiv="refresh" content="0; url=https://datasette.io/global-power-plants"></head><body> <p>Redirecting to <a href="https://datasette.io/global-power-plants">datasette.io/global-power-plants</a></p></body></html>
(I’m not sure if this will ever be served though, thanks to the other file:)
global-power-plants.datasettes.com/_redirects
/global-power-plants* https://datasette.io/global-power-plants*:splat 301/* https://datasette.io/global-power-plants 301
Claude helped with this. Cloudflare redirects are executed in order, so the more specific one sending anything that starts with /global-power-plants
to https://datasette.io/global-power-plants
comes before the catch-all one for the top level.
Configuring it on Cloudflare
All that was left was to tell Cloudflare to deploy it from GitHub.
Cloudflare want you to use Workers instead of Pages now, but I ignored them and clicked around until I found the old Pages interface.
Click “Workers & Pages” in the menu, then the blue “Create” button:
Swich to the “Pages” tab:
Select “Import an existing GitHub repository” and fill out this form:
Because my site is static HTML I just needed to provide a project name and the path to the folder with my index.html
and _redirects
files in it.
And that was the configuration! It showed me a reassuring log file, including noting that my redirect rules were valid:
Finally I navigated to the “Custom Domains” tab and entered the subdomain and domain I wanted.
Because the DNS was managed by Cloudflare they configured the required CNAME for me automatically: