We are hosting a static site on Cloudflare workers and have HSTS enabled. However, I can not get
example.com to redirect to www.example.com using page rules following the Cloudflare tutorial here:
This tutorial covers redirecting example.com to www.example.com when using Cloudflare, to do the opposite, please see
Redirect www.example.com to example.com.
You should replace all instances of example.com in the screenshots with your domain.
Step 1, The DNS Record:
The first thing you will need is a DNS record for @, set to . This can point to any IP address as the redirection page rule will execute first. I would recommend pointing it to 192.0.2.1, a dummy IP.
Step 2, Th…
A. Workers Routes
On workers we have the following routes pointing to our worker:
B. Domain DNS
On the domain DNS there are two A records proxied through Cloudflare.
The first A record points
example.com to 220.127.116.11
The second A record points www to 18.104.22.168
Note I can not change the first record from
example.com to @, simply as the Cloudflare DNS interface is changes @ to example.com when I save the record.
C. Page Rules
We have two rules, one for www redirect and one for caching everything.
The first rule:
https://example.com/* forwarding URL 302 temporary redirect to https://www.example.com/$1
The second rule:
https://www.example.com/*, cache everything.