This guide explains the most common Edge Rule variables available and how to use them, with examples to help you build effective rules.
Edge Rules allow you to create powerful custom behaviors, like redirects, header modifications, or origin changes based on the incoming request. To make these rules dynamic and flexible, you can use variables that automatically adapt based on request data.
Variable expansion works in rules like:
- Redirect to URL
- Change Origin URL
- Set Request Header
- Set Response Header
You can mix these variables into your custom value using:
%{Collection.Key}
How to Access Edge Rules:
Navigate to CDN → Select your desired Pull Zone → Go to the Edge Rules tab.
Basic Variables
These are some of the most common and easy-to-use variables for general redirection and header logic
|
These are mostly used in older rules or simplified setups.
Examples:
Usage Tip: The {{ }} variable placeholders automatically include slashes where needed in URLs, so you don’t need to manually add / before or after them.
Advanced Variable Collections
The new variable system allows for more flexible and detailed access to request data using this syntax:
%{Collection.Key}
Note: When using these variable formats, you’ll need to manually include the / slashes in your URLs where appropriate, just like in the example images.
The example images are meant to illustrate how variables can be used. The way you combine and apply them may vary depending on your specific use case.
RequestHeaders
Access headers from the incoming HTTP request.
Example |
Result |
|---|---|
| %{RequestHeaders.Host} | videos.example.com |
| %{RequestHeaders.User-Agent} | Browser or client info |
Example Usage:
Query
Access specific query parameters in the URL.
URL Example |
Variable |
Result |
/video.mp4?user=123 |
%{Query.KeyName} |
123 |
Example Usage:
The "KeyName" field must be replaced with the key of your query so that the system obtains its value. So if your query is called "Myhash" and has a value of "123", for the system to obtain this value, you must call it like this: %{Query.Myhash}.
Path
Extract specific segments of the URL path. Useful for complex routing or origin overrides.
Example path: /hello/world/bunny/eat/carrot.jpg
Variable |
Output |
|---|---|
| %{Path.0} | hello |
| %{Path.1} | world |
| %{Path.0-2} | hello/world/bunny |
| %{Path.1-3} | world/bunny/eat |
| %{Path.3-} | eat/carrot.jpg |
| %{Path.1-} | world/bunny/eat/carrot.jpg |
| %{Path.-1} | hello/world |
Note: If the URL includes a query string and the path you extract doesn’t alter or truncate the filename, the query string will remain intact. For example:
If your path is:
/hello/world/bunny/eat/carrot.jpg?query=somethingand you use: %{Path.1-} The result will be: world/bunny/eat/carrot.jpg?query=something
Example Usage:
Url
Breaks down parts of the full URL
Key |
Description |
Example |
%{Url.Filename} |
Last part of URL. If the request is to a folder, this will remain empty |
video.mp4 |
%{Url.Extension} |
File extension |
mp4 |
%{Url.Directory} |
Folder path only |
/videos/2024/ |
%{Url.Hostname} |
Hostname from request |
|
%{Url.Path} |
Full path and query |
/videos/video.mp4?x=1 |
Example Usage:
User
Info about the user making the request.
Key |
Description |
Example |
|---|---|---|
%{User.IP} |
End-user IP address |
203.0.113.10 |
%{User.CountryCode} |
Country of the request |
DE |
Example Usage:
Server
Internal info about the server processing the request.
Key |
Description |
Example |
%{Server.ZoneCode} |
Code of the serving zone |
NY |
%{Server.ID} |
Server ID |
9482 |
Example Usage:
Practical Example: Domain redirects to www
If you want to automatically redirect all requests from domain.com to the www.domain.com version, while preserving the full path, you can use the following Edge Rule.
For example:
domain.com/test-website → www.domain.com/test-website
Rule Configuration:
- Rule Type: Redirect to URL
-
Value:
https://www.domain.com{{path}} - Status Code: 301
- Condition: Request URL
- Condition Value: *
Important:
This rule must be added to the pull zone where domain.com is declared, not where www.domain.com is
If both domain.com and www.domain.com are declared as hostnames in the same pull zone, you can narrow the redirect condition using:
*://domain.com*
This ensures the redirect only applies to the non-www version.
References
For more in-depth documentation and updated examples, visit: