Andre Franca

Using Cloudflare for Dynamic Url Redirects

Published:
Reading Time: 2 minutes

⚠ This post is over 2 years old and may contain outdated opinions, broken links, or incorrect information.

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">
<head>
    <title>403 Forbidden</title>
</head>
<body>
    <h1>403 Forbidden</h1>
</body>
</html>

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. example.com, instead of a subdomain, Cloudflare will require your old domain name nameservers to match Cloudflare’s.

Conclusion

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↩︎

Tags:

#Technology#Hosting Provider

Questions, comments or concerns?
Please share your thoughts through the Fediverse or Email.

<~ Previous: Website version 4.0

~> Next: We Need More Teaching on Digital Security

Articles from blogs I follow around the world wide web

Bots Are Eating My Blog for Lunch

I read this post while enjoying my first coffee this morning, and it piqued my interest. (link: …

via Kev Quirk - Posts Only Jun 11, 2025

Who writes the documentation?

If the future is AI writing code (or writing much of it) — who writes the documentation? Is it the developers reviewing the code? If developers have to review code and write documentation based on their understanding of what they've read, is that more …

via Posts feed • Cory Dransfeldt Jun 6, 2025

A stack of browsers

I was a very happy Arc user for a couple of years, but after The Browser Company announced they would no longer be working on it, I started to assess alternatives. Now I've ended up with a stack of browsers, instead of reaching for a single browser to…

via Rach Smith's digital garden Jun 1, 2025

Generated by openring