Andre Franca

Using Cloudflare for Dynamic URL Redirects

Published: January 31, 2023
604 words · 2 minutes to read
Tagged: #technology#web-hosting.

A couple of days ago, a domain switch took place on this website, and I wanted to have a way to dynamically1 forward URLs from the old domain to the new one, preserving the current permalink structure.

Searching the web, I came across the “Using Netlify for Dynamic URL Redirects” article, written by Brad Taunt. Problem is, I didn’t want to sign up for Netlify just for that.

My blog is already hosted on Cloudflare, so it made sense to me to use it for the redirects too. I checked their documentation to see if something similar could be achieved. And it is, in fact, really simple and free.

Steps to reproduce

Setup content locally

$ touch _redirects index.html 404.html
/  https://domain.tld  301
/* https://domain.tld/:splat 301

Replace domain.tld with your domain.

Breaking down the above lines:

~ First line: Redirects all connections from the old domain’s home page to the new domain one;

~ Second line: A splat (asterisk, *) will greedily match all characters and use them to dinamically redirect a user to the new location with :splat.

<!DOCTYPE html>
<html lang="en">
    <title>403 Forbidden</title>
    <h1>403 Forbidden</h1>

Create a new Cloudflare Pages project

You have three options to setup your project: connecting to a git repository (usually Github or Gitlab); uploading a folder; or using the Wrangler cli tool. We’re uploading our folder in this example.

Set up a custom domain

NOTE: If you’re redirecting from a zone apex, i.e., instead of a subdomain, Cloudflare will require your old domain name nameservers to match Cloudflare’s.


That’s pretty much it!

Cloudflare will be in charge of generating an SSL certificate for you, and by now, you’ll have all of your pre-existing URLs forwarded dynamically to your new domain.

TIP: A similar approach can be used to have your big URLs shortened to another small domain.tld. Of course, without all telemetry those URL Shortener services provide.

  1. Dynamically in this case means to preserve the current permalink structure, instead of DNS forward setup, which doesn’t allow this level of customization. 

  2. Feel free to add to your _redirects file a different set of rules. All options are well documented here

Questions, comments or concerns?
I'd love to hear your thoughts! Please share them through email or Mastodon.

<~ Previous: Website version 4.0

~> Next: We Need more Teaching on Digital Security

Articles from blogs I follow around the world wide web

Bring Back the Humble Forum

Social media is fucking us all up. I’d love to go back to the days of forums, where there was no popularity contests, or “influencers”. I’ve been ruminating about my use of social media a lot recently and to be honest, I’m still not that …

via Kev Quirk July 26, 2023

The web I want

The web I want is powered by passion, not money. The web I want doesn't need to ask for permission to track me because they know that tracking me is not the right thing to do. The web I want doesn't make me waste bandwidth by loading pointless vid…

via Manuel Moreale RSS Feed July 25, 2023


I worked in a cafe that was a family-run business. The owner's mother would tell us, the employees, how to mop the floor. It wasn't the most efficient way to do it, but she insisted this particular set of steps was THE way to clean the floor. My c…

via Rach Smith's digital garden July 17, 2023

Generated by openring