title: varnish cache http 301 302 header location redirects
body: <p>I'm trying to set up an in-the-middle varnish server to allow a Rails app on xyz.com to work on several other domain names.</p>
  
  <p>What happens is that from time to time the Rails app outputs some 301/302 redirects and apparently varnish does not change these headers, so visitors get redirected to the original site (which is behind the public-facing varnish server), so... error.</p>
  
  <p>Is there a way to configure this rewrite on the varnish side of things?</p>
  
  <p>under vcl_fetch I tried the following:</p>
  
  <pre><code>if ( (beresp.status == 301) || (beresp.status == 302) ) {
      set req.url = regsub(req.url,".*",regsuball(regsub(beresp.http.Location,"^http://[^/]+(.*)","\1"),"[+]","%2520"));
      return(restart);
  </code></pre>
  
  <p>But maybe I don't understand how this works exactly? Any help will be much appreciated</p>