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 ~ "^www"

Matches if 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.


You can also use req.url.ext to find the file extension specified in a URL. For example, in the request, 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 =; # now equals 'abbbccccc'
4 set resp.http.match1 =; # now equals 'a'
5 set resp.http.match2 =; # now equals 'bbb'
6 set resp.http.match3 =; # now equals 'cccccc'

The[0-9] objects allow you to capture matches. The 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");

You can use to greatly simplify the previous example:

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

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

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

Replacing content

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

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

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

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

1Host: ->
4Host: ->
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.