VCL regular expression cheat sheet

Fastly VCL uses a subset of Perl Compatible Regular Expression (PCRE) syntax. This is case sensitive and forward slashes don't need to be escaped. To disable case sensitivity, add (?i) to the start of your expression.

Basic matching

req.url == "/phrase"

Matches only if req.url is exactly /phrase.

req.url ~ "phrase"

Matches phrase anywhere.

Matching at the beginning or end of a string

req.http.host ~ "^www"

Matches if req.http.host starts with www.

req.url ~ "\.jpg$"

Matches if req.url ends with .jpg.

Multiple matches

req.url ~ "\.(png|jpg|css|js)$"

Matches if req.url ends with either .png, .jpg, .css, or .js.

req.url ~ "\.php(\?.*)?$"

Matches if req.url ends with .php, .php?foo=bar or .php?, but not .phpa.

NOTE

You can also use req.url.ext to find the file extension specified in a URL. For example, in the request www.example.com/1/hello.gif?foo=bar, req.url.ext will contain gif.

req.url ~ "\.[abc]server$"

Matches if req.url ends with .aserver, .bserver or .cserver.

Matching wildcards

req.url ~ "jp.g$"

Matches if req.url ends with jpeg, jpag, and jp0g, but doesn't match if req.url ends with jpg. It also matches if any other character is between the jp and the g.

req.url ~ "jp.*g$"

Matches jp followed by 0 or more random characters ending with the letter g (jpeg, jpg, and jpeeeeg all match).

Capturing matches

1set req.http.Foo = "abbbccccc";
2if (req.http.Foo ~ "^(a+)(b+)(c+)") {
3 set resp.http.match0 = re.group.0; # now equals 'abbbccccc'
4 set resp.http.match1 = re.group.1; # now equals 'a'
5 set resp.http.match2 = re.group.2; # now equals 'bbb'
6 set resp.http.match3 = re.group.3; # now equals 'cccccc'
7}

The re.group.[0-9] objects allow you to capture matches. The re.group.0 object evaluates to the entire matched string even if no capture groups have been supplied. You can use these objects to replace this example:

1if (req.url ~ "(?i)\?.*some_query_arg=([^&]*)") {
2 set req.http.Thing-I-Want = regsub(req.url, "(?i)\?.*some_query_arg=([^&]*).*", "\1");
3}

You can use re.group to greatly simplify the previous example:

1if (req.url ~ "(?i)\?.*some_query_arg=([^&]*)") {
2 set req.http.Thing-I-Want = re.group.1;
3}

You could even get really fancy and do something like this:

set req.http.Thing-I-Want = if(req.url ~ "(?i)\?.*some_query_arg=([^&]*)", re.group.1, "");

Replacing content

set req.http.host = regsub(req.http.host, "^www\.","");

Removes a leading www. from the host, if present.

set req.http.api-test = regsub(req.http.host, "^www\.","api.");

Sets the api-test header to contain the host-header, but replaces a leading www. with api.:

1Host: www.example.com ->
2Host: www.example.com
3api-test: api.example.com
4Host: example.com ->
5Host: example.com
6api-test: example.com
set req.url = regsuball(req.url, "/+", "/");

Changes all occurrences of multiple slashes in the URL to a single slash. For example, //docs///intro.html will be transformed to /docs/intro.html.

Was this guide helpful?

Do not use this form to send sensitive information. If you need assistance, contact support. This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.