Varnish Redirection

http, redirect, url, varnish

Solution

If you want to do this in Varnish 4.0, the way to do it have changed a bit

#default.vcl

sub vcl_recv {
  if (req.req.url~ "^/details.php?$") {
    return (synth (750, "")); #This throws a synthetic page so the request won't go to the backend
  }
}

sub vcl_synth {
  if (resp.status == 750) {
    set resp.status = 301;
    set resp.http.Location = "http://bdnews24.com";
    return(deliver);
  }
}

Problem

Recently I moved to a new system based on Java from PHP platform. The new website has pretty URLs such as - http://mysite.com/science/2013/03/22/universe-is-older-than-previously-thought The old website had URLs like -mysite.com/details.php?cid=37&id=239411 For search engine results we need to redirect all these URLs containing /details.php? to the homepage, say urlredirect.com. I have been looking at these examples https://www.varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL and came up with the following in redirect.vcl of my Varnish configuration. In vcl_recv function - ``` if(req.url~ "^/details.php?$" ) { error 301 "Moved Temporarily"; } ``` But I am confused what should be there in vcl_error function? For now it is like this - ``` else if(obj.status == 301 && req.url~ "^/details.php?$"){ set obj.http.Location = "http://bdnews24.com"; return (deliver); } ``` I think it's simple as that? still it would be great to share experience with someone who has done this.

Original source