Cloudflare Redirects
There have been a couple of scenarios recently where I thought some additional subdomains and/or path redirects would be useful.
- I always forget the capitalisation in the URL for my OpenAPIVisualiser tool, so wanted a redirect from “devwithimagination.com/oav” to the full URL for the project
- While I have a “.scot” domain for my profile at the moment, I wanted to have “about.devwithimagination.com” redirect to it.
This post, like many of mine, exists as a reminder to myself how this is setup as it took me a few reads of the documentation to understand this and when I did I found that I’ve done this before.
There seems to be two approaches that might be valid solutions:
It turns out I’ve been down this road before - I’ve already got a page rule configured for the naked apex domain redirecting to www. On the free plan, you are allowed 3 page rules without paying for more.
If we ran out of page rules, or needed a more complex logic, you could potentially use Cloudflare workers for redirects.
The Cloudflare documentation is pretty good, but as a worked example the second of my scenarios is the more complicated one.
As this is a new subdomain I first needed to create the DNS entry for about.devwithimagination.com. I just created this as a CNAME record pointing to the domain it (may) eventually replace and left it as proxied. The proxied part is important for letting the page rule work.
Then going to Rules -> Redirect Rules and configuring a single redirect.
And that’s it. I repeated this second step for my other redirect and now both my scenarios are handled with minimal configuration within Cloudflare.