Page rule to redirect all domain and its sub

Hello, please how do i create a page rule to redirect any link starting with either **http :// ** or **www . ** to **https://** for the parent domain and its sub.?

For instance:

**www.example.com** -> **https://example.com**
**www.example.com** -> **https://example.com**
**www.sub.example.com** -> **https://sub.example.com**

you can do it with either page rules or bulk redirects, personally I’d use bulk redirects

first of all Cloudflare automatically redirects HTTP to HTTPS as long as the DNS entries are orange-clouded and your settings are sensible

second of all, www.sub.example.com might give you issues because Cloudflare normally generates SSL certificates for example.com and *.example.com, which only covers one level of subdomain. (And SSL certs don’t support multiple wildcards like ..example.com). You might be able to catch it while it’s still HTTP and forward it properly then but if it’s already HTTPS you’re likely to get a certificate error.

Doing it with page rules you could do something like this:

For the subdomain you could even do a wildcard like this:

Using bulk redirects you could do like this (not gonna try to do a wildcard subdomain match on this one):

Use temporary (302/307) redirects until you’re certain it’s how you want it then change to permanent (301/308)

2 Likes

I tested the page rule setup on one of my own domains to verify it worked (with the caveat about www.sub mentioned above)

http://www.example.com/, https://www.example.com/, and http://www.subdomain.example.com/ all forward perfectly; https://www.subdomain.example.com/ gets a certificate error as expected; if you’re on the free Cloudflare plan I don’t think you can get a suitable certificate using only Universal SSL. If you have full control over your origin, you might be better off grey-clouding the www.subdomain.example.com DNS entry, let the traffic hit your origin, and have your origin do the forwarding. Or point to a different origin that can do it, doesn’t really matter, the server you do the forwarding on doesn’t have to be the same server where the traffic ends up. On your origin you should be able to use LetsEncrypt / certbot to get a valid certificate for www.subdomain.example.com.

Personally I have a $2/month VPS that I do most of my forwardings on (among other things), it’s definitely one of the best investments you can have if you don’t already have a server that you have full control over.

I also tested it using bulk redirects (as shown in my 1st post) instead of page rules and it worked just as well, with the same caveat about the https://www.subdomain.example.com/ certificate

I personally try to avoid using multiple levels of subdomains if at all possible. Do you have reason to believe that people will actually attempt to use www.subdomain.example.com, like are there already links out in the wild? I normally don’t even bother trying to catch https://www.subdomain.example.com traffic, kinda a waste of effort in most cases

1 Like