Skip to content

Instantly share code, notes, and snippets.

@ncserny
Last active December 25, 2015 17:39
Show Gist options
  • Save ncserny/7014367 to your computer and use it in GitHub Desktop.
Save ncserny/7014367 to your computer and use it in GitHub Desktop.
Varnish: Easily redirect "olddomain.com/..." to "newdomain.com/..." URLs at the cache level.
# Redirect "olddomain.com/..." to "newdomain.com/..." URLs at the cache level.
# Just set an error code (in our case 750) in vcl_recv when accessing old urls, then define the redirect in vcl_error.
# Tested with Varnish 3.X
sub vcl_recv {
# Set error code when accessing an old url.
if (req.http.host ~ "^(www\.)?olddomain\.com$") {
error 750;
}
}
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = "http://www.newdomain.com" + req.url;
set obj.status = 302;
return(deliver);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment