Help with Page Rule redirects for Subdomains

Hello! Hoping you can help :slight_smile:

​I need some assistance/guidance for redirecting ~300 subdomains to a different subdomain, using page rules. I’ve read through the community and docs, can’t seem to find an answer…

For example… https://name1.example.com needs to redirect to … https://name1.agent.example.com

I thought we had this figured out, and set up the dns records, and a wildcard redirect in page rules… but the behavior is not as intended. When the page rule is turned on, it breaks the root domain… We only want the subdomains to redirect.

We have DNS entry of each subdomain, with the orange cloud. For example: name.example.com. 1 IN A 64.246.164.131

The Page Rule that has been setup is to match the URL: https://*.example.com/ and Forward to: http://$1.agent.example.com/

Is there something different I should be doing for my URL match in the Page Rule? Is there another page rule I can add to prevent the root domain from being affected? Please help! :smiley:

Thanks!

Hi,

You could try creating a couple of page rules for the root domain and place it before the wildcard page rule:

1 redirects from naked to www

URL: example.com/*
Forward to: https://www.example.com/$1

2 for the www subdomain, with a setting just to make it not trigger the wildcard page rule

Setting: Automatic HTTPS Rewrites

3 the wildcard page rule. Since your subdomains may have more than one URL, you may want to change your rule slightly:

URL: *.example.com/*
Forward to: https://$1.example.com/$2

You don’t need to add the HTTP/S in the source URL, only in the target.

Awesome - thanks @floripare. We now have this sorted out and your advise was helpful.

I also needed on additional redirect to make all subdomains always use HTTPS because I needed to have the forwarding URL include HTTPS in the URL for Cloudflare to consider it a valid page rule.

So, my page rules are as such, which works!

URL: example.com/*
Forward to: https://www.example.com/$1
URL: www.example.com/*
Automatic HTTPs Rewrites: On
URL: *.example.com/*
Automatic HTTPs Rewrites: On
URL: https://*.example.com/*
Forward to: http://$1.subdomain.example.com/
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.