I wanted to run Google Ads for one of my side projects, but I noticed a problem: Google was displaying my domain as www.example.com, even though I hadn’t set up a www subdomain. This meant that if anyone tried to visit www.example.com, they’d find nothing. Since I’m not using a reverse proxy and rely on Cloudflare (CF) rules to direct traffic, I figured the best solution was to set up the www subdomain and redirect www to non-www on the Cloudflare side. It takes two things: a DNS record for www, and a rule that forwards it to the root domain.
If you leave the record on DNS only (grey cloud), Cloudflare never sees the request, and the www redirect will not work.
www.example.com/*. This pattern catches all URLs starting with www.example.com.https://example.com/$1. The $1 ensures that any subdirectory path from the original URL is appended to the new URL.Cloudflare also offers Redirect Rules in the same “Rules” section. They take the same source pattern and destination, so use them if your account no longer shows Page Rules.
The rule works in both directions. To redirect the root domain to www instead, set the pattern to example.com/* and the destination to https://www.example.com/$1.
Any request to www.example.com now redirects to example.com, and subdirectory paths are preserved.
Occasional notes on software, tools, and things I learn. No spam.
Unsubscribe anytime.