Page Rule Forward Url not working for root of site when set using api

I have the following site proxied in Cloudflare:

I am trying to apply the following page rules to it:
If Url matches https://* .test.excelwraps.com/* forward url via temporary redirect to ExcelWraps - Technology
If Url matches https://*.test.excelwraps.com/ forward url via temporary redirect to ExcelWraps - Technology

The problem is that it will forward anything below the root of the site e.g. https://guide.test.excelwraps.com/[anything] but not just https://guide.test.excelwraps.com i.e. the root of the site remains undirected. I have tried changing the order but it has no effect. I have tried prefixing both sites with http/ https but makes no difference.

Can anyone tell me what I am doing wrong?

Either post screenshots or properly escape your code parts, right now the configuration is incomprehensible due to the forum’s formatting.

What code parts? What is unclear?

Please have a look at your initial posting and the URLs.

Simply post a screenshot of your page rules.

In this case your second page rule will never execute as it always stops at the first one. I presume this is not what you intended. Shouldnt the first one already do the trick?

Can you describe what exactly you want to achieve? Post a few examples.

Yes I shouldn’t need the second one, the first one should do everything I need but doesn’t. I want to forward:

https://guide.test.excelwraps.com/mypage.htm (this works)
https://guide.test.excelwraps.com (this doesn’t work)

Interesting, it really does not seem to work.

I just tried the same setup and it did work as expected.

Right now your page rules are set up exactly as depicted in the screenshot? Not that it should make any difference, but try disabling #2 anyhow.

Done. I have only the first rule deployed. Annoying isn’t it?
and yes, they get set up by script so I can reproduce them at will.

I am afraid I dont really have an explanation for that. From the looks it should work (and it does work in my case).

Only guess I would have is whether there could be any e.g. whitespace character which tampers with the match. Try removing the page rules and set up the first from scratch, paying attention that you dont enter (or copy paste) anything that could distort the match.

Should that still not work, I would suggest to open a support ticket. Maybe there is something about the host level within the domain (my test was a first level host, yours is a second level).

If I create it manually it works as you say. If I use the api it doesn’t. I also added the rule manually and then used the api to recreate the rule and it says a matching rule already exists so there is something wrong with the api.

Manually it works, via the API it does not?

That would definitely be a case of support then.

Ok. I will raise a call. This is what I am posting:

{"targets":[{"target":"url","constraint":{"operator":"matches","value":"*.test.excelwraps.com/*"}}],"actions":[{"id":"forwarding_url","value":{"url":"https://excelwrapsgeneral.z35.web.core.windows.net/?redirectTo=$1.test.excelwraps.com%2F$2", "status_code":302}}],"status":"active"}

against this url:
https://api.cloudFlare.com/client/v4/zones/[my zone id]/pagerules

Here is the full powershell code. I am using powershell 5.1.18362.145

Write-Host ("Creating page rule")
$forwardingRule = '{"targets":[{"target":"url","constraint":{"operator":"matches","value":"*.' + $SiteBaseUrl + '/*"}}],"actions":[{"id":"forwarding_url","value":{"url":"https://excelwrapsgeneral.z35.web.core.windows.net/?redirectTo=$1.' + $SiteBaseUrl + '%2F$2", "status_code":302}}],"status":"active"}'
try {
    $result = Invoke-RestMethod -Method POST -Uri $cloudFlareUrl -Headers $cloudFlareApiHeaders -Body $forwardingRule
} catch {
    # Workaround because Invoke-RestMethod swallows any error message
    $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
    $errorResponse = $streamReader.ReadToEnd() | ConvertFrom-Json
    $streamReader.Close()
    $errorResponse.messages
}  
Write-Host ("Forwarding rules complete")

Generally it shouldnt matter whether you set it up manually or via the API. As long as the API returns successfully, and the page rule shows up correctly, you should be fine.

If that is not the case here, it really is something only support can elaborate on.

Post the ticket number you got, so @cloonan can track it.

1 Like

Ticket number is (#1757750)

1 Like

If it helps anyone. It turns out I had another rule (Cache level: bypass) which was interferring with the rule I was trying to set up (forward url). For some reason this meant the root of the site was not forwarded but everything else was. All I had to do was change the priority of cache level: bypass to be lower than the forwarding rule and all urls on the domain were forwarded correctly.

I just noticed you actually did not post a full screenshot of your page rules. This is the reason why it always is a good idea to post the full picture and why I asked for a screenshot of your page rules.

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