Redirect Policy
The redirect policy is used to redirect a specific URL to another URL. The HTTP status code for redirection can be either "301 Permanent Redirect" or "302 Temporary Redirect."
How to Determine if a URL is Redirected¶
There are two ways to determine whether a URL is redirected within the CDN system:
-
Prefix: This type of match applies when the URL starts with a specified path that matches a given value. For example, a URL path of
/images
will apply the policy to all files within the/images
directory. Additionally, customers can set policies for specific files by explicitly entering the file path as a prefix, e.g.,/files/mydownload.zip
will apply the policy only tomydownload.zip
. -
Regex (Regular Expressions): This allows for very flexible policies by defining file paths in advance. For example, a regex
/video/.+.mp4
will match all files in the/video
directory with a.mp4
extension. The CDN analyzes the requested URL to determine whether a policy rule should be applied. Care should be taken when using regex matching for URL paths; regular expressions are powerful but should be used carefully to avoid unintended consequences.
There are different regular expression syntaxes that look similar but have key differences. The CDN policy uses Perl-style syntax.
Info
For more information on regex, this website provides a helpful introduction: Regular-Expressions.info.
Explanation of Some Regex Values¶
Special Characters¶
Regular expressions combine text and patterns. Patterns are defined using a set of special characters: . [{} () \ * + ? | ^ $
If any of these special characters need to be used literally in a pattern, they must be escaped with a backslash (\
). The backslash indicates that the character is part of the string and not a special pattern character.
Example
A regular expression /animated$files
will not match the actual path /animated$files
unless it is written with a backslash: /animated\$files/
Wildcards¶
The simplest form of regular expression matching uses wildcards. In regex syntax, a dot (.
) matches any character. This can be extended by adding a repetition qualifier (*
, +
, or ?
).
-
.*
matches zero or more characters. -
.+
matches one or more characters. -
.?
matches zero or one character.
These multipliers can also be applied to text and character ranges.
Ranges¶
A pattern can match a range of characters by enclosing them in square brackets []
. This can include individual characters or a range of characters.
Example
[123456789]
is equivalent to [1-9]
.
[a-zA-Z0-9]
matches any letter or digit (uppercase or lowercase).
You can also combine these with multipliers: [a-z]+
matches one or more lowercase letters.
Alternates¶
Alternation allows matching multiple options. For example, you can create a pattern that matches three specific file extensions. Alternation options are listed as (<pattern1>|<pattern2>|<pattern3>)
.
Alternation options can be plain text or regular expressions.
Anchoring¶
Like prefix rules, regular expressions must match the leading /
in a path. A simple pattern like filename.txt
will not match anything.
Regular expressions must match from the beginning to the end of the path. For example:
/file
matcheshttp://host/file
but nothttp://host/file.txt
.
Some common examples:
-
/files/august
matches only the path/files/august
. This is different from using a prefix because it matches only this specific path and not any subdirectories. -
/files/.+/index.html
matches allindex.html
files under the rootfiles
directory, regardless of location. -
/files/august/.+\.(mp4|ogg|swf)
matches all files in the/files/august
directory with extensions.mp4
,.ogg
, or.swf
. Note the\
before the.
to ensure it is treated as text rather than a special character.
Multiple Matches¶
If more than one policy matches a URL, the most specific policy will be applied.
First, prefixes and regular expressions are compared to determine the most specific match. If they are equivalent, subnet IP and location are checked.
When comparing two prefixes, the longer prefix is considered more specific.
When comparing two regular expressions, the length of the leading text before any pattern is checked first. The most specific regular expression has the longest leading text.
When comparing a regular expression and a prefix, the length of the leading text in the regular expression is compared to the prefix length, and the longer one is considered more specific.