regsub in error response

Caunter, Stefan scaunter at topscms.com
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, "(.*)",
"/mobile\1");
                        error 750 "Moved Temporarily";
                }

Then handle it in vcl_error:

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

You can specify obj.status = 301 of course.

Stefan Caunter :: Senior Systems Administrator :: TOPS
e: scaunter at topscms.com  ::  m: (416) 561-4871
www.thestar.com www.topscms.com


-----Original Message-----
From: varnish-misc-bounces at varnish-cache.org
[mailto:varnish-misc-bounces at varnish-cache.org] On Behalf Of Michael
Alger
Sent: September-11-10 5:52 AM
To: varnish-misc at varnish-cache.org
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 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.

_______________________________________________
varnish-misc mailing list
varnish-misc at varnish-cache.org
http://lists.varnish-cache.org/mailman/listinfo/varnish-misc




More information about the varnish-misc mailing list