Creating Cloudflare redirects
I was having issues sorting out the redirects on Cloudflare and have learnt a few things on the way so am documenting them here – because I will forget it all whilst in the pursuit of happier things.
Directory migration
I made a major change to the structure of a section of my site /shorts/ has reverted back to /short-articles/ as I am testing out a possible issue with this sites current lack of SERP visibility in Bing. As such I needed to revert the redirects from /short-articles/ to /shorts/ back again. Not usually an issue for my skills but I thought I'd make some notes.
Cloudflare hosting
This site is currently hosted on Cloudflare pages, auto deployed from its github repository. I can use the inbuilt redirect solution that Cloudflare provides but that has a very limited capacity so I have created a file named _redirects that Cloudflare recognises (as does Netlify) and uses for redirects. This is in my 11ty setup and has a passthrough so that it is output in the build exactly the same as I have written it.
This file can have up to 2,000 redirects if properly formatted. I have had issues with only being able to use 120 redirects but this was because I had built some of them incorrectly.
Deployment logs
One source of info that is really useful is the deployment log files. Find it here: Cloudflare > Compute (Workers) > your site > View details
Now scroll down in the Build log and it will tell you all sorts of interesting things such as time and what happened. This includes parsing the redirects file.
10:41:26.990 Parsed 83 valid redirect rules.
The log helpfully tells you redirects that have not been implemented and also provides some optimisation suggestions such as the order of the redirects.
Notes on Cloudflare redirect rules best practices
Exclamation mark on the end of the rule
I think this was a leftover from Netlify's redirect rules
http://simoncox.com/* https://www.simoncox.com/:splat 301!
Valid status codes are 200, 301, 302 (default), 303, 307, or 308. Got 301!.
I did a find and replace and removed the exclamations marks.
There was another issue with this particular rule though!
Only relative URLs are allowed
I had some domain redirects in place, because you can't have enough domains, but Cloudflare doesn't like them as the source URL, they have to be a part of the directory structure, so I had to take them out.
https://www.tagsoup.net/* https://www.simoncox.com/:splat 301!
Only relative URLs are allowed. Skipping absolute URL https://www.tagsoup.net/*.
I am now forwarding those domains using an external method.
Redirect rule spaces and or tabs
Cloudflare is quite particular about the number of spaces - it looks for gaps in the redirect rule of 2 and 3 sets of spaces or tabs. If there are 4 or more sets of spaces and the rule will be ignored.
/blog/category:Model Railways /narrow-gauge-modelling/ 301
Expected exactly 2 or 3 whitespace-separated tokens. Got 4.
This means if your URL has a space you will need to use %20 in place of the space:
/blog/category:Model%20Railways /narrow-gauge-modelling/ 301
Colons interpreted as placeholders
I have some URLs with colons in them, such as /blog/category:things, and have put these under section 2 as the colon is interpreted as a placeholder, or you can URL-encode the colon of course!
/blog/category%3AModel%20Railways /narrow-gauge-modelling/ 301
Order of redirect rules
For performant reasons it is better to list static redirects before dynamic ones so that Cloudflare’s systems can run these more effectively:
- Static rules - /home, /about, /contact
- Rules with placeholders - /blog/:slug, /users/:id
- Rules with splats - /old-site/*
I split these up using # comments in my redirect files.
Final thoughts about the Cloudflare redirect file rules
I originally thought I could switch the sites hosting between Cloudflare and Netlify hosting by just changing the DNS, a very handy and quick solution, but I am not going to have to keep two sets of redirect files if I want to do this as the rules are quite different between them.
Having learnt more about the Cloudflare redirect rules If am much happier running the rules from this file. Also, this started off as a shorticle but is long enough to be a full article.
You can read more on Cloudflare’s full documentation on redirects and let me know if I have missed anything important out!
Previous short: Apple OSX Messages full stops emoticon bug