If you need to do an HTTP redirect for anything while using Cloudflare the first idea that comes to mind will be just to use Redirect Rules, however, if your goal is to redirect a path, like /old/*
to /new/*
, you actually can’t do that in the free plan, as regex_replace
requires at least a PRO subscription.
Luckily this blog is hosted on Cloudflare pages, which has its own redirect mechanism, and you can do such redirects without needing a pro subscription.
using the _redirects file
you just need to create file called _redirects, which needs to be included in the final directory uploaded to Cloudflare pages. There are multiple site generator dependent ways to do this, I just add it manually in GitHub Actions after the site generation step.
Using Splats
The easiest way to do this is just using splats, a Splat is everything that is matched when using *
after a path.
_redirects
|
|
using Placeholders
Placeholders are another option, they do the same thing, but look cleaner I guess.
However, they have some specific limitations, so stick to splats when possible.
_redirects
|
|
sources
https://developers.cloudflare.com/pages/configuration/redirects/
https://community.cloudflare.com/t/transform-rule-replace-part-of-url/437813