The PHP Page Rule Checker of CloudFlare


The Page Rule is a very powerful tool in CloudFlare, you can define up to 3 page rules in Free Plan, and Up to 20 in Pro Plan. The additional page rules are only $5 per 5 rules. The page rules take priority over the global settings, and for each URL, only 1 page rule will be activated, which means that you need to sort the page rules in the priority descending order i.e. the most specific rules are listed first.

Sometimes, we want to know which page rule is triggered given a URL, instead of going through each rule by rule from the top, we can use the following PHP utility. Let’s see the example first:

1
2
3
4
$ php rulechecker.php https://helloacm.com/api/what-is-my-ip-address/?cached
Page Rules for https://helloacm.com/api/what-is-my-ip-address/?cached:
1:  https://*helloacm.com/api/*/?cached*
2:  https://*helloacm.com/api/*
$ php rulechecker.php https://helloacm.com/api/what-is-my-ip-address/?cached
Page Rules for https://helloacm.com/api/what-is-my-ip-address/?cached:
1:  https://*helloacm.com/api/*/?cached*
2:  https://*helloacm.com/api/*

The rulechecker utility for CloudFlare is written in PHP and it will check each given valid URL to see if it matches any page rule (the first rule is the one that is actually activated while the other rules are for references only). Here is another example that you can specify more URLs.

1
2
3
4
5
6
7
$ php rulechecker.php https://helloacm.com/api/what-is-my-ip-address/?cached https://justyy.com/top https://codingforspeed.com/abc
Page Rules for https://helloacm.com/api/what-is-my-ip-address/?cached:
1:  https://*helloacm.com/api/*/?cached*
2:  https://*helloacm.com/api/*
Page Rules for https://justyy.com/top:
1:  https://*justyy.com/top*
**No Page Rules Match for https://codingforspeed.com/abc
$ php rulechecker.php https://helloacm.com/api/what-is-my-ip-address/?cached https://justyy.com/top https://codingforspeed.com/abc
Page Rules for https://helloacm.com/api/what-is-my-ip-address/?cached:
1:  https://*helloacm.com/api/*/?cached*
2:  https://*helloacm.com/api/*
Page Rules for https://justyy.com/top:
1:  https://*justyy.com/top*
**No Page Rules Match for https://codingforspeed.com/abc

CloudFlare Page Rule Pitfalls

If the page rule does not start with http:// or https:// then by default a regular expression pattern e.g. /^https?:\/\/ will be added. So, the page rule https*helloacm.com is being evaluated with the following logic:

http://https*helloacm.com
https://https*helloacm.com

If you are declaring the protocol as part of a page rule you need to use the full protocol http:// otherwise this is parsed as part of the URL.

1
2
3
if (!preg_match('/^https?:(.*)$/i', $pagerule)) {
    $rr = "https?:\/\/" . $pagerule;
}
if (!preg_match('/^https?:(.*)$/i', $pagerule)) {
    $rr = "https?:\/\/" . $pagerule;
}

The PHP Page Rule Checker of CloudFlare

The PHP Page Rule Checker of CloudFlare is implemented in PHP, based on the CloudFlare API. To use this, you need to find out the APP_KEY from your cloudflare account dashboard. Then after properly configuring the cloudflare.php, the rule checker can be used immediately at the command line. You can chmod +x rulechecker.php to give it a execute permission and then you can use the rule checker like this:

1
2
3
$ ./rulechecker.php https://codingforspeed.com/archives-of-pagesposts/
Page Rules for https://codingforspeed.com/archives-of-pagesposts/:
1:  https://*codingforspeed.com/archives-of-pagespost*
$ ./rulechecker.php https://codingforspeed.com/archives-of-pagesposts/
Page Rules for https://codingforspeed.com/archives-of-pagesposts/:
1:  https://*codingforspeed.com/archives-of-pagespost*

The core idea of this rule checker is to fetch the list of the page rules for that zone (e.g. domain, parsed from the given URL). The page rules are checked one by one with the widechar replaced by (.*), which matches any single character in regular expressions.

The full PHP source code of PHP CloudFlare Rule Checker can be found at github.

cloudflare-page-rules The PHP Page Rule Checker of CloudFlare API cloud cloudflare CloudFlare php PHP

cloudflare-page-rules

CloudFlare Technology

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
692 words
Last Post: How to Fix Microsoft PowerPoint Attempting-to-Repair-but-Fail Problem? (cannot open)
Next Post: How to Parallel For in Linux Bash Shell?

The Permanent URL is: The PHP Page Rule Checker of CloudFlare

Leave a Reply