Preface
I ran a bunch of tools to check if my blog is SEO-friendly. One of the recommendation was to canonicalize the URLs by avoiding multiple addresses pointing to the same page. In my case, that was serving my website at both https://www.simonko.dev, as well as https://simonko.dev. The solution is rather simple - redirect www to non-www. My website is fully configured using Terraform. Since I’ve never done it before with Cloudflare Terraform provider, I thought I might share what I did.
I am not using the latest version of the provider due to a bug, as described in my blog post about how I automated my blog, so depending on the version you use, it may be slightly different, but the principle remains.
The implementation
In Cloudflare, the redirect is achieved by defining a Cloudflare Pages Rule. The rule is supposed to match www.simonko.dev/*
, and, if that matches, respond with a 301 and a redirect to simonko.dev/$1
, where $1
is the matched wildcard piece.
Here’s how it’s done with Terraform:
|
|
Please note that this operation requires Zone - Page Rules - Edit
permissions granted for your token, otherwise you may get an error like the one I got:
Error: failed to create page rule: Unauthorized to access requested resource (9109)
with cloudflare_page_rule.www_to_non_www_redirect,
on pagerules.tf line 1, in resource "cloudflare_page_rule" "www_to_non_www_redirect":
1: resource "cloudflare_page_rule" "www_to_non_www_redirect" {
…and that’s really all there’s to it.
I’m keeping this post short.
Validation
Once configured, I ran a few checks to confirm the rule is working as expected:
|
|
Additional comment
Some SEO-checkers classified this problem as “Duplicate content”, where they could access my posts using either www or non-www domain. Others called this “canonicalization issues”. The explanation provided by these tools is that some search engines may be “unsure” about which is the correct one to index. I am not sure if this was a problem in practice, as the links on my blog always pointed to a non-www version, regardless of the URL used to access the page. Yet, I decided to fix it to get a higher score, he-he - and to make sure it doesn’t become a problem in future. ;)
Thanks
Thank you for reading my blog. If you've found this post useful, please consider supporting me. ;)