regsub in error response

Caunter, Stefan scaunter at
Tue Sep 14 19:46:34 CEST 2010

You need to tell the browser the new location in vcl_error. I have
something similar for mobile redirects.

First in vcl_recv catch the event:

  if( ... some condition ... ) {
                        set req.url = regsub(req.url, "(.*)",
                        error 750 "Moved Temporarily";

Then handle it in vcl_error:

if (obj.status == 750) {
                set obj.http.Location = req.url;
                set obj.status = 302;

You can specify obj.status = 301 of course.

Stefan Caunter :: Senior Systems Administrator :: TOPS
e: scaunter at  ::  m: (416) 561-4871

-----Original Message-----
From: varnish-misc-bounces at
[mailto:varnish-misc-bounces at] On Behalf Of Michael
Sent: September-11-10 5:52 AM
To: varnish-misc at
Subject: Re: regsub in error response

On Fri, Sep 10, 2010 at 10:12:59AM -0700, Chris Hecker wrote:
> I'd like to do a 301 from to, 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,"","");
> 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 = "" 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.

varnish-misc mailing list
varnish-misc at

More information about the varnish-misc mailing list