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 😅
Initially I 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:
/posts/
path on the domain.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/
)Let’s take a look at how we do this:
First, we’ll navigate to the relevant place in CloudFlare
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:
())
In the Dashboard console it will look something like this:
The Redirect Rules entry for catching incoming requests
Now that we have caught the relevant incoming requests, let’s transform the old format into our new format.
We want to do a dynamic redirect to the new location, in the new format which is lowercase and without any .html
at the end.
Let’s set up a redirect rule of type Dynamic
, give it a status code of HTTP 301 (i.e. "Moved Permanently”) and use the following expression:
) ))
Breaking this down from inner-to-outer, what we are doing:
http.request.uri.path
part of the request (e.g. /posts/a-blog-post.html
of https://codetalk.io/posts/a-blog-post.html
)/posts/A-Blog-Post.html
becomes /posts/a-blog-post.html
)substring
from the start, 0
, to 5 characters back from the last character, -5
(e.g. /posts/a-blog-post.html
becomes /posts/a-blog-post
)concat
(e.g. /posts/a-blog-post
becomes https://codethoughts.io/posts/a-blog-post
)We’re avoiding any complex rewrite expressions, such as regex, because it’s a) not actually needed and b) not available on the free plan.
In the Dashboard console it will look something like this:
The Redirect Rules entry for dynamically redirecting the incoming requests
Hope that helps if you’re ever running into the same!