Redirecting URLs with CloudFlare

Christian Kjær
3 min readJul 31, 2024

Not a Medium member? Read the post here for free.

I recently moved my blog from https://codetalk.io (now my commercial site) to https://codethoughts.io. This of course also meant, that everything linking to my old blog was now broken, which is not the best experience for any readers that I might have 😅

I initially considered setting up rewrite rules somewhere for each individual post, but then thought of a much smarter way, using CloudFlare’s Redirect Rules, to instead redirect all direct links to posts and throw them to the new location on codethoughts.io.

The documentation for these rules are a bit sparse, so I thought I’d share how I did. First though, let’s set some context:

  • The base domain changed from https://codetalk.iohttps://codethoughts.io
  • All posts live under the /posts/ path on the domain
  • All post slugs are identical in dates and names
  • We no longer have a .html ending on the new blog (i.e. what previously might have been https://codetalk.io/posts/a-blog-post.html would now be https://codetalk.io/posts/a-blog-post/)
  • We’re on the free CloudFlare plan, so there’s a limit to which expressions we can use

Let’s take a look at how we do this:

Catching our requests on codetalk.io

--

--

Christian Kjær

Founder of codetalk.io and blogging on codethoughts.io 👀 •• Rust/Serverless ❤️ Previously: Director of Eng at Famly 🌱 Built IoT platform at Factbird ☁️