Answering the "rewrite" part (please see the comments as that could be easier), you have 2 options:
- Perform a client redirect (preferred IMHO) [a]
- Rewrite the host internally [b]
See also:
- https://www.varnish-software.com/static/book/build/exercises/complete-rewrite_urls_and_headers.html?highlight=redirect
- https://www.varnish-cache.org/trac/wiki/RedirectsAndRewrites
- https://www.varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL
[a]
sub vcl_recv {
// ...
if ( req.http.host == "domain.com" ) {
error 750 "http://www." + req.http.host + req.url;
}
// ...
}
sub vcl_error {
// ...
if (obj.status == 750) {
set obj.http.Location = obj.response;
# Set HTTP 301 for permanent redirect
set obj.status = 301;
return(deliver);
}
// ...
}
[b]
sub vcl_recv {
// ...
if ( req.http.host == "domain.com" ) {
set req.http.host = "http://www." + req.http.host;
}
// ...
}