How to configure www subdomain to redirect to and serve Pages content

Hello all. I have spent several hours trying multiple permutations of the settings I think should work, but haven't been successful. I am hoping that someone on here can help me. DESCRIPTION AND WHAT'S CURRENTLY WORKING I have a new domain fully activated on cloudflare, a cloudflare pages project successfully built and deployed to a pages.dev URL, and a DNS cname record pointing from my Apex domain to mypages.dev URL through a proxy. This all works correct and the apex domain is correctly serving content as I would expect. CURRENT PROBLEM AND WHAT I'VE TRIED I now want the www subdomain URL to redirect to the apex domain URL and serve the same pages.dev content. i.e., I type https://www.example.com/ into the browser address bar, I get a 301 redirect to https://example.com/ and my pages project renders I have: 1. added a cname DNS record for the www subdomain and pointed it at the pages.dev URL, also proxied. 2. Added a bulk redirect rule so that the www subdomain redirect to the Apex domain with a 301 permanent status. 3. And I have tried adding and removing the www. Subdomain to my cloudflare pages project' Custom Domains list. Neither configuration works. When I add my www subdomain as a pages custom domain, then the subdomain URL does resolve correctly... But it doesn't get redirected. And in that scenario I'm basically serving the same pages.dev content on both the Apex domain and the sept domain... Which I don't want because that's not canonical and can lead to confused SEO. And when I remove the subdomain as a pages custom domain, I receive a 522 connection timed out response. If I don't add a cname record for the subdomain, then DNS doesn't even know what to do with the www URL. Any help or advice would be greatly appreciated. Thanks!
26 Replies
Hard@Work
Hard@Work2w ago
Cloudflare Docs
Redirecting www to domain apex
Learn how to redirect a www subdomain to your apex domain (example.com).
kazzb
kazzbOP2w ago
Hello and thanks for responding 🙂 I have not tried the exact instructions on that page. Specifically, I have not tried using an A record and an IP address as the Target. I've used a CNAME and mypages.dev as the target URL. So I guess I'm not sure if this support page is curated specifically for Cloudflare Pages scenarios. Because when using A records, an IP address is required. And Cloudflare recommends not using actual infrastructure IPs. At least that's my understanding...
Hard@Work
Hard@Work2w ago
In general, that's the case. 192.0.2.1 is considered an internal loopback IP address, and if you just created an A record pointing to it, you would get an error. However, when a Bulk Redirect Rule is added, the rule will handle the request before Cloudflare ever attempts to hit 192.0.2.1. It is, in effect, used as a dummy IP address
kazzb
kazzbOP2w ago
So I tried adding the A record for "www" pointing to 192.0.2.1. I don't think I'm actually seeing any difference. Just a 522 Connection Timed Out Response
Hard@Work
Hard@Work2w ago
Do you also have the Bulk Redirect rule?
kazzb
kazzbOP2w ago
I do. I have a bulk redirect rule with a list of one entry. Source: https://www.example.com/* Target: https://example.com/$1
Hard@Work
Hard@Work2w ago
What's the domain?
kazzb
kazzbOP2w ago
DM'ed you
Hard@Work
Hard@Work2w ago
Could you try an AAAA record with 100::? I don't think that would fix anything, but can't hurt to try. Also, can you screenshot your Bulk Redirect rule/List?
kazzb
kazzbOP2w ago
Hard@Work
Hard@Work2w ago
Sorry, without the question mark Just 100::
kazzb
kazzbOP2w ago
Ahh took that too literally. Should have known that. Working on getting you the bulk redirect screenshot AAAA record added, old A record removed Still getting 522 on the www subdomain it looks like DM'ed the Bulk Redirect List screenshots
Hard@Work
Hard@Work2w ago
If you put www. into https://dash.cloudflare.com/?to=/:account/trace, do you see a Bulk Redirect Rules invocation?
kazzb
kazzbOP2w ago
No, I don't see the Bulk Redirect Rule getting matched during a trace. Sent you DM Should I change my www subdomain DNS record back to a CNAME pointing at my Pages URL? It's currently still set to the AAAA
Hard@Work
Hard@Work2w ago
That shouldn't be the issue, let me see if I can find someone more knowledgable to ask...
kazzb
kazzbOP2w ago
Yeah, changed it back to the A with the loopback and the CNAME, and no change. Trace shows 522 Some sources that I've read say to add both the apex and the www subdomain as "custom domains" on my Cloudflare Pages project, and that there should be some way to set one as primary. Then if the non-primary gets hit, the www, it auto redirects to the primary. But I see nothing like that.
Hard@Work
Hard@Work2w ago
I don't think that is a thing? If you do set it up, to my knowledge, it would serve from both, instead of redirecting
kazzb
kazzbOP2w ago
Yeah, exactly. that's my experience. Maybe that's an old config
Hard@Work
Hard@Work2w ago
Can you try doing the bulk redirect URL without https:// in the source? So www.example.com -> https://example.com
kazzb
kazzbOP2w ago
What about the wildcard stuff? remove that as well? OLD: https://www.example.com/* -> https://example.com/$1 Kept the wildcard stuff and a trace still showed a 522 with no bulk redirect match. I'll give it another 5 minutes or so and try againin case the setting hasn't propogated
Hard@Work
Hard@Work2w ago
It should look something like this, just in reverse
kazzb
kazzbOP2w ago
I got it to work ... I used a redirect rule instead of a bulk redirect rule I navigated to Overview -> Rules in the left panel. Several recommended "templates" were suggested. One of which was "Redirect from www to to root". That created a single redirect rule that used a wildcard pattern Request URL: https://www.* Target URL: https://${1} (with 301 redirect) Just tried this as well and it worked. So perhaps the wildcard stuff was throwing it off. Did a trace and the bulk redirect rule matched finally. Bulk Redirect Rule: Source URL: www.example.com/ Target URL: https://example.com Status: 301 Thanks so much for your help @Hard@Work And in both successful scenarios (bulk redirect rule and single rediret rule), I had a CNAME for the www -> the "pages.dev" Cloudflare Pages project url.
Hard@Work
Hard@Work2w ago
Happy to help, and sorry for the loops there
kazzb
kazzbOP2w ago
All good! It's always in the details... Really appreciate it.
Chaika
Chaika2w ago
late but yea with Bulk Redirect stuff there's no wildcard support, if you want it to grab the path, you check "Subpath matching" and then if you want it to spit the path onto the end, "Preserve Path Suffix" next to Edit Parameters
kazzb
kazzbOP2w ago
Amazing... That makes sense. Thanks so much 🙏

Did you find this page helpful?