Portal Home > Knowledgebase > CDN > Fastly > Varnish RegEx cheat sheet

Varnish RegEx cheat sheet

in order to set the correct RegEx for conditions in Fatsly please refer to this cheat sheet

(by , 2016)

Regular expression cheat sheet for Varnish


This document was written years ago, for Varnish version 2.0.

Varnish has SWITCHED regex engine from posix regex to pcre, and is now case
sensitve. There are also numerous other changes. The basic principles still
hold true, but you are probably better off using official documentation
instead of something I wrote during a lunch break for participants of a
Varnish training course back in 2011.


Varnish regular expressions are NOT case sensitive. Varnish uses POSIX
regular expressions, for a complete guide, see: "man 7 regex"

Basic matching:
	req.url ~ "searchterm"
	True if req.url contains "searchterm" anywhere.

	req.url == "searchterm"
	True if req.url is EXACTLY searchterm

Matching at the beginning or end of a string ~ "^www."
	True if starts with "www" followed by any single
	character. ~ "^www\."
	True if starts with "www.". Notice that . was

	req.url ~ "\.jpg$"
	True if req.url ends with ".jpg"

Multiple matches
	req.url ~ "\.(jpg|jpeg|css|js)$"
	True if req.url ends with either "jpg", "jpeg", "css" or "js".

Matching with wildcards
	req.url ~ "jp.g$"
	True if req.url ends with "jpeg", "jpag", "jp$g" and so on, but NOT
	true if it ends with "jpg".

	req.url ~ "jp.*g$"
	True if req.url ends with "jpg", "jpeg", "jpeeeeeeeg",
	"jpasfasf@@!!g" and so forth (jp followed by 0 or more random
	characters ending with the letter 'g').

Conditional matches
	req.url ~ "\.phg(\?.*)?$"
	True if req.url ends with ".php" ".php?foo=bar" or ".php?", but not
	".phpa". Meaning: Either it ends with just ".php" or ".php"
	followed by a question mark any any number of characters.

	req.url ~ "\.[abc]foo$"
	True if req.url ends with either ".afoo" ".bfoo" or ".cfoo".

	req.url ~ "\.[a-c]foo$"
	Same as above.

Replacing content
	set = regsub(, "^www\.","");
	Replaces a leading "www." in the Host-header with a blank, if

	set req.http.x-dummy = regsub(, "^www.","leading-3w.");
	Sets the x-dummy header to contain the host-header, but replaces
	a leading "www." with "leading-3w" example:

	Host: => 

	Host: =>

Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Fastly - Use free SSL (Views: 2674)


Improve your web performance, reduce costs and boost ROI!

Contact us today