title: How to add trailing slash with varnish?
body: <p>I want to add a trailing slash to all URL's with varnish (via 301 redirect).</p>
  
  <p>I was surprised I couldn't find anything about online anywhere, however.</p>
  
  <p>This was the closest I got, but is obviously broken because it doesn't account for query strings or or anything with a . in it.</p>
  
  <pre><code>if (req.url !~ "/$") {
    return (synth (751, ""));
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>sub vcl_synth {
    if (resp.status == 750) {
      set resp.status = 301;
      set resp.http.Location = "http://www.example.com" + req.url;
      return(deliver);
    }
  }
  </code></pre>
  
  <p>Test cases I want to account for</p>
  
  <p><code>example.com/xyz?query=string</code> -> <code>www.example.com/xyz/?query=string</code> (add www, add /)</p>
  
  <p><code>example.com/api/latest.json</code> -> <code>www.example.com/api/latest.json</code> (add www, dont add /)</p>