Varnish 3
in sub vcl_recv add close to the top
if (req.http.host == "www.mysample.com" || req.http.host == "my-sample.com" || req.http.host == "www.my-sample.com") {in sub vcl_error add
set req.http.host = "mysample.com";
error 750 "http://" + req.http.host + req.url;
}
if (obj.status == 750) {
set obj.http.Location = obj.response;Varnish 4
set obj.status = 301;
return(deliver);
}
in sub vcl_recv add
if (req.http.host ~ "^www.mysample.com") {in sub vcl_synth
return (synth (750, ""));
}
if (resp.status == 750) {Actually I like the implementation in Varnish 4 better. As you can make all the related changes at one place instead at 2 locations in Varnish 4. This also helps improving your memory used as only a single option is stored in cache instead of one for www.mysample.com/index.html and another for mysample.com/index.html
set resp.status = 301;
set resp.http.Location = "http://mysampele.com" + req.url;
return(deliver);
}
Hope this helps someone
Source
How to redirect non-www URLs to www in Varnish