How to Redirect Apex Domain to www in Cloudflare
Publish date: Jul 5, 2024
Last updated: Jul 28, 2024
Last updated: Jul 28, 2024
1. Set up DNS records
Create corresponding DNS A or CNAME records for the apex and subdomain.

2. Use Cloudflare DNS
Go to your registrar and add the Cloudflare Nameservers.

3. Create a redirect rule
Go to Redirect Rules and create a new rule.
When incoming requests match
Using the Expression Editor:
(lower(http.host) eq "example.com")
Then
Type: Dynamic
Expression: concat("https://","www.example.com",http.request.uri.path)
Status code: 301

Ref:
Cloudflare Docs, Redirect the apex domain to a subdomain