Overview

Most cdn users only use basic caching features. In fact, CDN edge rules can do much more: custom cache policies, URL redirects, request header rewriting, mobile adaptation, and more.

This article introduces common application scenarios and configuration methods for CDN edge rules.

I. What Are CDN Edge Rules?

Edge rules are a series of condition judgments and operations executed on CDN nodes. When a request reaches a CDN node, the CDN processes the request according to the rules you set, and then decides whether to return cached content, go back to the origin, or perform other operations.

Common edge rule capabilities include:

  • Set different cache times based on URL path or file type
  • Redirect mobile requests to a mobile page based on User-Agent
  • Return different content based on country or region
  • Modify HTTP request headers or response headers
  • URL rewrite and redirect

II. Scenario 1: Set Different Cache Times for Different Directories

Different directories on a website have different update frequencies. Images and CSS under /static/ may only be updated once a month, while product pages under /products/ change every day.

Configuration example (using Cloudflare rules as an example):

  • Rule 1: URL path contains /static/ → cache time 30 days
  • Rule 2: URL path contains /products/ → cache time 1 hour
  • Rule 3: URL path contains /admin/ → no cache

By setting different cache policies for different directories, you can maximize cache hit rate while ensuring users see the latest content.

III. Scenario 2: Automatic Mobile Redirect

If your website has a separate mobile version (such as m.example.com), you can automatically detect device type and redirect through edge rules.

Configuration logic:

  1. Determine whether the User-Agent is from a mobile phone or tablet
  2. If the user visits the desktop URL (example.com), automatically redirect to the mobile version (m.example.com)
  3. You can add an exception rule to allow users to click "View Desktop Version"

This avoids device detection in website code and reduces origin server load.

IV. Scenario 3: URL Rewrite to Hide Real Paths

Through URL rewriting, complex dynamic URLs can be converted into user-friendly static URLs. For example:

Original URL: /product.php?id=12345

Display URL: /product/12345

After configuring URL rewrite, the address users see is more concise, and the origin server does not need to modify code. It is commonly used to optimize SEO and improve user experience.

Unlike 301 redirects, URL rewriting is an internal forward. The browser address bar does not change, and it does not affect search engine indexing.

V. Edge Rule Support Across CDNs

CDN Number of Rules Supports Conditional Logic
Cloudflare (Free) 3 page rules Basic condition matching
Cloudflare (Pro) 20 rules Complex conditions (including regex)
Upyun Varies by plan Supports condition matching
Alibaba Cloud CDN Multiple configurable Supports conditional rules
Tencent Cloud EdgeOne (Free) Limited number Supports basic conditions

Free plans usually have a limited number of rules. It is recommended to prioritize configuring the most critical cache rules.

VI. Points to Note

  • Edge rules execute in order; pay attention to the priority relationship between rules
  • The number of rules is limited (free plans usually 3–10), so prioritize core rules
  • After modifying rules, wait for CDN node synchronization (usually a few minutes)
  • Complex regular expressions may affect CDN performance; keep rules concise

By properly configuring edge rules, you can achieve cache optimization, traffic scheduling, and user experience improvement without modifying origin server code.