Member-only story
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:
Catching our requests on codetalk.io
First, we’ll navigate to the relevant place in CloudFlare
- Go into the CloudFlare dashboard
- Select your domain (e.g. codetalk.io)
- Expand Rules in the sidebar
- Select Redirect Rules
- Create a new rule
- Give it an appropriate name, and make sure to select Custom filter expression.
In my case here, I specifically want to limit the logic to when the URI Path
starts with the value /posts
. CloudFlare will neatly demonstrate that this rule will generate the expression:
(starts_with(http.request.uri.path, "/posts"))
In the Dashboard console it will look something like this:
