The rules feature provides a graphical interface. The rules identify parameters in user requests to determine whether a configuration applies, giving you flexible and precise control over your configuration. You can create and deploy conditional rules for features, such as cache, redirection, compression, origin fetch, and WAF, using the same syntax and logic.
ESA rules
Rules let you create and deploy conditional rules across different features, such as cache, redirection, compression, origin fetch, and WAF, using the same basic syntax and configuration logic.
Notes
For a specific feature, if you add configurations at both the global level and the rule level, the rule configuration takes precedence when a user request matches the rule conditions.
A single rule configuration, which includes rule conditions and the feature configuration, is limited to 4 K characters.
All configuration information for a site, including global and rule configurations, is limited to 512 K characters. This limit excludes rule configurations for security protection.
A rule name is limited to 128 characters.
Rule templates
ESA provides rule configuration templates for common scenarios. You can click a template to quickly create a rule.
After you add a rule, when a user requests a resource, ESA matches and executes rules sequentially based on the rule execution priority.
Procedure
In the ESA console, choose Websites, and in the Website column, click the target site.
In the navigation pane on the left, choose .
On the Rule Templates page, select Redirect to a New URL and click Create in the upper-right corner.
In the custom rule settings, enter the URI path to redirect, such as
/esa.jpg.In the URL redirect section, enter the full redirection URL, such as
https://www.aliyun.com.Test the access by visiting
http://yourDomain/esa.jpg. A 301 status code is returned, which indicates that the redirection is active.
Use rules for WAF protection
Example: Block all countries and regions except the Chinese mainland
Configure rule conditions
To block all countries and regions except for a single one (the Chinese mainland in this example), perform the following steps:
In the ESA console, choose Websites, and in the Website column, click the target site.
In the navigation pane on the left, choose .
Enter a Rule Name, such as
rule-01.From the Match Type drop-down list, select Country/Region.
From the Operator drop-down list, select does not equal.
From the Match Value drop-down list, select Chinese Mainland.
Finally, from the Action drop-down list, select Block, and then click OK.

To block a single country or region, select equals from the Action drop-down list. Then, follow the same steps.
Expression: (ip.geoip.country ne "CN").
Verify the blocking rule
If a request originates from a region outside the Chinese mainland, such as Singapore, the default block page and a 403 status code are returned.

Set a redirection rule
Example: Redirect requests from one URL to another
Configure rule conditions
In the ESA console, choose Websites, and in the Website column, click the target site.
In the navigation pane on the left, choose .
Enter a Rule Name, such as
rule-02.From the Match Type drop-down list, select Hostname.
From the Operator drop-down list, select equals.
In the Match Value text box, enter the hostname
www.example.com.Add another rule expression for the URI path. Click And, and from the Match Type drop-down list, select URI Path.
From the Operator drop-down list, select equals.
In the Match Value text box, enter the path
/image1.jpg.In the URL Redirect section, enter the URL to redirect to:
https://www.example.com/image/image2.jpg.

Verify the redirection
Accessing the original URL redirects the request to the path /image. The 301 status code confirms the redirection to image.jpg.

Set a browser cache time-to-live rule
Example: Cache resources for 1 hour if the hostname is www.example.com and the URL path is /content
Configure rule conditions
In the ESA console, choose Websites, and in the Website column, click the target site.
In the navigation pane on the left, choose .
Enter a Rule Name, such as
rule-03.From the Match Type drop-down list, select Hostname.
From the Operator drop-down list, select equals.
In the Match Value text box, enter the hostname
www.example.com.Add another rule expression for the URI path. Click And, and from the Match Type drop-down list, select URI Path.
From the Operator drop-down list, select equals.
In the Match Value text box, enter the path
/content.

Configure the browser cache TTL
Select Use Custom TTL. Enter 1 and select Hours as the unit.

Verify the browser cache TTL
Test access to the file. The response header cache-control: max-age=3600 is returned.
