Redirecting URLs with CloudFlare
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.io → https://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 beenhttps://codetalk.io/posts/a-blog-post.html
would now behttps://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: