CNAME record: www to non-www

Hi,

Just checking out my cloudflare dns records (set up by someone else) and noticed my custom url is using a cname record to point to cloudflare’s development url.

So example.com is an alias of the development page: example.pages.dev.
Cloudflare seems happy with that, but i also noticed they missed the www record.

Do I also alias the www address to example.com with a CNAME record? I’m just a little confused as if I also alias example.pages.dev there wouldn’t be any way of stating that the non-www is canonical, would there?

Thanks,

Nick

For a Pages site, don’t add DNS records yourself. When you add the custom domain to the Pages project, the DNS is handled for you. You can add the www name to the project as an additional custom domain. This will make both names work. In your Dashboard at the account level go to your Pages/Workers projects, select the project, and add the custom domain there.

If you want to redirect www to the bare domain, go to Redirect Rules in the Dashboard and create a rule like this:

For ease of copying and pasting, the Expression in the bottom field is: concat("https://example.com", http.request.uri.path) (replace with your domain obviously).

This will redirect any www request to the bare domain, keeping any path intact.

4 Likes

Hi i40west,

Thanks for the quick reply! I’m guessing when you say the DNS is handled for me you mean the A records and anything else related to the URL? I only ask because I’ve got a lot of other DNS records that I don’t want Cloudflare touching! :smile: Should I make a note of all the other records incase Cloudflare gets a bit excited? :smiley:

Thanks for the easy copy paste rule as well - just to double check: http.request.uri.path references the uri after the domain, right? (example.com**/this-here**)

Thanks again, appreciate it,

Nick

1 Like

DNS records are auto-created when you add custom domains to a Pages project. They won’t get drunk and go randomly changing anything else. :joy: You just have to make sure to add any names you want the site to respond to. The www name is a whole separate name from the base domain so if you want both you need to add both.

Yes, the uri.path is the part after the slash. It probably doesn’t matter in your case because you didn’t already have a www name, so there aren’t existing www URLs out there, but with that rule if someone goes to www.example.com/foo they will get redirected to example.com/foo.

1 Like

It would be just my luck!
Thanks again though, massive help,

Nick

weird. I went to set up the custom domain and it says it’s already active :man_shrugging:
does cloudflare not put a records there for you to see? it just works in the background?

additionally it’s now also saying:

image

all i did was remove that cname record :confused:

Sorry for the 3 for 1 response :smile:

no no no, the CNAME that was already there was fine.

Right now your bare domain is resolving, but not www.

You should be able to add www.sub1.io as a custom domain to the Pages project, under Custom Domains. It tells you it’s already active? Is there anything in your DNS records for www? If you can’t add it to the Pages project, post a screenshot of your DNS settings.

ah, sorry mb, I misunderstood. I thought the presence of the CNAME record meant it had been set up manually, rather than relying on cloudflare

Added both again, looks good.

Is it not strange though that I’ve got a CNAME from www to sub1prod.pages.dev and a redirect from www to the bare url?

guess the sites going to be down for a bit though :slight_smile:

Not down, it works from here, both regular and www. All done!

1 Like

Actually the redirect isn’t working. The www version is loading normally, so you have the situation you mentioned in your original post where neither one is canonical.

Can you post a screenshot of your redirect rule?

Sorry I was slow to add the rule. Should be good now!

1 Like

lol, i didn’t change example.com :smile:

Okay, one last thing: you pasted in my example but didn’t replace example.com with your domain.

edit: I see you figured it out :joy:

yup, i noticed straight away. in my defence it’s late here :grimacing:

1 Like

Thanks again for the help!

1 Like

Not down, it works from here, both normal and www. All finished!