Redirect traffic with Cloudflare (Page Rules vs Redirect Rules)

stylised image of a diversion and 'road ahead closed' road sign.

I wrote a few posts (and scripts) last year focused on Mastodon, how to install it, and various surrounding ideas, which were all based on what I'd learnt while creating and running my own instance. A year or so later, and while running the instance was a fun project, the cost and time required for very little benefit (a single user instance) led me to decide to shut the instance down.

I've already migrated my account to using Mastodon's mechanism to notify/move your followers to a new account, and while I'm sure there aren't many hits to the previous URL, it seems like bad practice to shutter a service and not at least redirect users.

As it's a single user instance, I want to redirect all traffic to the domain, and using Cloudflare's free redirect rules or page rules, this is very easy to do.

Enable Proxy

The caveat to all of the following steps is that traffic needs to run through Cloudflare for the rules/redirects to take effect. Cloudflare can just be used for DNS entries (and there are times where you might want to just use this) so if the page rules/redirects aren't working, make sure you see orange 'proxied' icon for the domain A record.

Screenshot showing a DNS A record in the Cloudflare web panel. The orange proxied icon shows traffic is running through Cloudflare's servers.

Redirect Rules

Within the section for that domain in the Cloudflare console, choose Rules -> Redirect Rules, then 'Create Rule'. Name the rule and choose 'All incoming requests'. Set the redirect to 'Static'. Enter the URL to redirect to, set the status code to 301 and click 'Deploy'.

Screenshot showing the creation of a broad Cloudflare dynamic redirect
The 301 status code indicates that this is a permanent redirect. If for some reason you were temporarily redirecting users then you should choose 302.

All traffic will now redirect to the new account. If I want to specify a different redirects depending on the destination (i.e., my account URL redirects to a different location than the root domain) then you can use Page Rules...

Page Rules

Instead of Redirect Rules, a Page Rule can be used to either the same effect, or for more granular control.

For a simple redirect, under Rules, choose Page Rules -> 'Create Page Rule'.

For the URL, enter yourdomain/*, e.g.* and then in the settings box, choose 'Forwarding URL'. Set the redirect status code to 301/302 as appropriate, and set the destination address to your new location, then click 'Save and Deploy Page Rule'.

Screenshot showing a broad redirect of all traffic using Cloudflare's Page Rules.

Multiple Page Rules

As Cloudflare gives you 3 page rules even on a free account, you could create multiple rules. Realistically, I only need to redirect visits to @techbits and direct post links to the new Mastodon account. Anything else I want to redirect elsewhere (I may even redirect to this post, because, why not?)

To do this, create two rules: One for* to the new account, and then another for* to the alternate location. If you create them in a different order, make sure to change them so the more specific rule is triggered first, otherwise everything will get picked up by the broader rule.

screenshot showing two Cloudflare pages rules, the first which redirects the @user account to the new Mastodon account, and the other that redirects all other requests to a different location.

What next?

With this rules in place, I can decommission the server without worrying about inbound traffic. The next step is to create a Ghost rule to rewrite all of the instances of the old url/account to new!

For a bit more infomation of Cloudflare Page Rules specifically, see this previous post.

If there's anything I've missed/messed up, please let me know in the comments, or send me a message at @techbits@su....oh yeah... or @techbitsio.

Great! Next, complete checkout for full access to
Welcome back! You've successfully signed in
You've successfully subscribed to
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated