清漆重定向

时间:2013-03-24 10:06:56

标签: http url redirect varnish

最近我从PHP平台迁移到基于Java的新系统。新网站有漂亮的网址,例如 -

http://mysite.com/science/2013/03/22/universe-is-older-than-previously-thought

旧网站的网址如-mysite.com/details.php?cid=37&id=239411

对于搜索引擎结果,我们需要将包含 /details.php?的所有这些网址重定向到主页,例如urlredirect.com。我一直在查看这些示例https://www.varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL,并在我的Varnish配置的redirect.vcl中提出了以下内容。

在vcl_recv函数中 -

 if(req.url~ "^/details.php?$" ) {
 error 301 "Moved Temporarily";
  }

但是我很困惑vcl_error函数应该有什么?现在就是这样 -

  else if(obj.status == 301 && req.url~ "^/details.php?$"){
    set obj.http.Location = "http://bdnews24.com";
    return (deliver);
  }

我觉得这很简单?与那些做过这件事的人分享经验真的很棒。

2 个答案:

答案 0 :(得分:8)

如果你想在Varnish 4.0中这样做,那么这样做的方法已经改变了一点

#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);
  }
}

答案 1 :(得分:5)

最好创建一个自定义错误代码,然后在那里发送重定向URL,而不必在vcl_error中重复自己。一个简短的例子:

在vcl_recv中:

set req.http.x-Redir-Url = "http://newdomain.com" + req.url; 
error 750 req.http.x-Redir-Url;

在vcl_error中:

if (obj.status == 750) {
    set obj.http.Location = obj.response;
    set obj.status = 301;
    return(deliver);
}