Skip to main content
Edge Rules support dynamic variables that automatically adapt based on request data. These variables allow you to create flexible rules for redirects, header modifications, and origin changes.

Supported actions

Variable expansion works in the following Edge Rule actions:
  • Redirect to URL
  • Change Origin URL
  • Set Request Header
  • Set Response Header

Variable syntax

There are two variable syntaxes available:
SyntaxFormatExample
Basic{{variable}}{{path}}
Advanced%{Collection.Key}%{User.CountryCode}
The {{ variable }} syntax automatically includes slashes where needed in URLs. When using %{Collection.Key} syntax, you need to manually include slashes in your URLs.

Basic variables

These simple variables are commonly used for general redirection and header logic:
VariableDescriptionExample Output
{{path}}Full request path including query string/videos/test.mp4?user=1
{{hostname}}The hostname from the requesttest.b-cdn.net
{{country_code}}Two-letter country code of the user’s IPUS
{{query_string}}Query string only (without the ?)user=1
{{request_method}}HTTP method usedGET, POST
{{file_name}}Filename from the URL (last part after /)file.jpg, test.mp4

Example: Redirect preserving path

To redirect all requests from one domain to another while preserving the path:
SettingValue
ActionRedirect to URL
Redirect URLhttps://www.example.com{{path}}
Status Code301

Advanced variable collections

The advanced variable system provides detailed access to request data using the %{Collection.Key} syntax.

RequestHeaders

Access headers from the incoming HTTP request.
ExampleResult
%{RequestHeaders.Host}videos.example.com
%{RequestHeaders.User-Agent}Browser or client info

Query

Access specific query parameters from the URL. For a URL like /video.mp4?user=123:
VariableResult
%{Query.user}123
Replace KeyName with your actual query parameter name. For example, if your query is ?Myhash=123, use %{Query.Myhash} to get 123.

Path

Extract specific segments of the URL path. This is useful for complex routing or origin overrides. For the example path /hello/world/bunny/eat/carrot.jpg:
VariableOutputDescription
%{Path.0}helloFirst segment
%{Path.1}worldSecond segment
%{Path.0-2}hello/world/bunnyRange from index 0 to 2
%{Path.1-3}world/bunny/eatRange from index 1 to 3
%{Path.3-}eat/carrot.jpgFrom index 3 to end
%{Path.1-}world/bunny/eat/carrot.jpgFrom index 1 to end
%{Path.-1}hello/worldFrom start up to (but not including) index 1
If the URL includes a query string and the path you extract doesn’t alter the filename, the query string remains intact. For example, with /hello/world/bunny/eat/carrot.jpg?query=something, using %{Path.1-} returns world/bunny/eat/carrot.jpg?query=something.

Url

Access parsed components of the full URL.
VariableDescriptionExample
%{Url.Filename}Last part of URL (empty for folders)video.mp4
%{Url.Extension}File extensionmp4
%{Url.Directory}Folder path only/videos/2024/
%{Url.Hostname}Hostname from requestcdn.mysite.com
%{Url.Path}Full path and query/videos/video.mp4?x=1

User

Information about the user making the request.
VariableDescriptionExample
%{User.IP}End-user IP address203.0.113.10
%{User.CountryCode}Country of the requestDE

Server

Internal information about the server processing the request.
VariableDescriptionExample
%{Server.ZoneCode}Code of the serving zoneNY
%{Server.ID}Server ID9482

Request

Information about the HTTP request.
VariableDescriptionExample
%{Request.Method}HTTP methodGET, POST
%{Request.Path}Full URL path and query/videos/video.mp4?x=1
%{Request.QueryString}Query string onlyx=1

Practical examples

Redirect non-www to www

Redirect all requests from domain.com to www.domain.com while preserving the path:
SettingValue
ActionRedirect to URL
Redirect URLhttps://www.domain.com{{path}}
Status Code301
ConditionRequest URL
Condition Value*
This rule must be added to the pull zone where domain.com is declared, not where www.domain.com is declared.If both hostnames are in the same pull zone, narrow the condition to *://domain.com* to only match the non-www version.

Route requests based on path segment

Route requests to different origins based on the first path segment:
SettingValue
ActionChange Origin URL
Origin URLhttps://%{Path.0}.example-backend.com/%{Path.1-}
ConditionRequest URL
Condition Value*/api/*
This would route /api/users/123 to https://api.example-backend.com/users/123.

Add country code to request header

Pass the user’s country to your origin for geo-based logic:
SettingValue
ActionSet Request Header
Header NameX-User-Country
Header Value%{User.CountryCode}
ConditionRequest URL
Condition Value*