This configuration example of Varnish will process inbound requests and use permanent redirects to force clients to get the canonical URI. For example, forcing a specific hostname by stripping the www. (which is the right way to do things, www. is so 2000-late). As and example, notice that google.com forces the www. prefix, while twitter.com forces the removal of www. host-prefix.
Removing www.
If a request comes in with the wrong host name, tag it and pass the request to the error handler. This is a good approach for one or two hostnames.
sub vcl_recv { if (req.http.host != "domain.tld") { error 601 req.url; } # ... } sub vcl_error { if (obj.status == 601) { set obj.http.location = "http://domain.tld" obj.response; set obj.status = 301; return (deliver); } }
Forcing www. prefix
This is what Google does,
sub vcl_recv { if (req.http.host != "www.domain.tld") { error 604 req.url; } # ... } sub vcl_error { if (obj.status == 604) { set obj.http.location = "http://www.domain.tld" obj.response; set obj.status = 301; return (deliver); } }