regsub in error response

Michael Alger varnish at mm.quex.org
Sat Sep 11 11:52:05 CEST 2010


On Fri, Sep 10, 2010 at 10:12:59AM -0700, Chris Hecker wrote:
>
> I'd like to do a 301 from a.com to b.com, but preserving the whole
> url,  but I can't figure out how to do it.  This doesn't work to
> send it to  vcl_error:
>
>  error 751 regsub(req.url,"a.com","b.com");
>
> The regsub isn't allowed there.  Can I stuff it in a header that I
> can  get in vcl_error?

You probably can. What I do for redirects is to rewrite the req.url
and then use that in the error statement, i.e.

  set req.url = regsub (req.url, "rewritefrom", "rewriteto");
  error 751 req.url;

For your particular case where you don't actually want to rewrite
the URL but instead only change the host part of it, I would do
something like:

  if (req.url ~ "(?i)^/vanitypath")
  {
    set req.url = "http://b.com" req.url;
    error 751 req.url;
  }

Note that the host isn't present in req.url (use req.http.Host for
that) - so in the first example, "rewriteto" should start with a
full protocol specification (http://...").

Most browsers work fine if they're told to redirect to /foo, but I
don't really like relying on it.




More information about the varnish-misc mailing list