Is this possible with Page Rules?


#1

I have 2 sites I am going to sign up with Cloudflare, let’s call them Alpha and Beta.

I want to set up page rules so that any request for the top level of the Alpha site gets redirected to www.Beta.com, but any request for a deeper link like www.Alpha.com/AnyDeeperLink just gets served as usual from Alpha. Is this possible in Cloudflare with Page Rules? Also, could this all be made to work whether or not the www is present?


#2

Hi, @ama,

You can try this page rule:

*alpha.com
Forwarding Redirect
301 - Permanent Redirect
https://www.beta.com

Please note: no dot before “alpha”, no slash after “com”.

If you have sub-domains other than www, for example, blog.alpha.com, you should instead create two rules, one for naked domain alpha.com and another for www.alpha.com.

Either way, you should create another rule to forward any visit to your plain *.alpha.com domain followed by a query string, such as www.alpha.com?utm_source=google.

This PR would be from:
*alpha.com/?*
to:
https://www.beta.com/?$2

Again, if you have another sub-domain other than www. you’d need to make two rules.

I hope this works! (It worked on a test website)


#3

Hi floripare,

Thanks for the info. Unfortunately, Cloudflare will not allow me to enter “*alpha.com” without a trailing slash. When I save the rule, it inserts the trailing slash whether I want it or not. Any thoughts on how to get rid of the trailing slash?

Thanks!


#4

I should also mention that the page rule with the trailing slash does not appear to work. Any requests for alpha.com just load alpha.com as before instead of https://www.beta.com

Also, I tried deleting the rule and re-adding with no luck, and I even deleted the whole domain from Cloudflare and re-added it, and weirdly the page rule came back (with the trailing slash) without me having to add it. Very frustrating.


#5

Forget the trailing slash, I thought it would make a difference, but not in your case. It should work with it.

Both source and target domains must be on Cloudflare. Please see this tutorial on how to properly set the DNS for the target domain, so that Cloudflare can make the Page Rule work:


#6

OK, I finally got this working the way I wanted, and for the benefit of anyone else trying to do this, here’s how.

The goal was to have requests for:

www.alpha.com
or
alpha.com

get redirected to beta.com, but allow requests like:

www.alpha.com/SomeDeepLink

to load from the alpha.com server as you would normally expect.

@floripare’s suggestion was correct in that I needed to have the alpha.com domain managed by Cloudflare and have the “orange cloud” turned on (turning it off breaks the redirection). However, I do NOT have the beta.com target domain even defined in Cloudflare, never mind orange clouded, and it still works.

I had been reluctant to turn on the orange cloud (CDN) because the alpha.com site is a SmugMug site, and I had read about how because SmugMug already uses Cloudflare as their CDN, turning on Cloudflare CDN (the orange cloud) for a particular SmugMug site caused no end of problems. Apparently this issue has been fixed.