How to add redirect exceptions using Varnish?

亡梦爱人 提交于 2019-12-13 03:27:28

问题


I am trying to redirect a path e.g. www.something.com/apple/pie to www.something.com/tickets/pie-details but also have some exceptions e.g. www.something.com/apple/helloworld does not get redirected to www.something.com/tickets/helloworld-details

This is what I have tried but doesn't work:

if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
    set req.url = "^/tickets/.*-details";
    error 701 req.url;
}

回答1:


https://info.varnish-software.com/blog/rewriting-urls-with-varnish-redirection

as an example (straight from the post):

sub vcl_recv {
    if (req.http.host != "www.varnish-software.com") {
        set req.http.location = "https://www.varnish-software.com/";
        return(synth(301));
    }
}
sub vcl_synth {
    if (resp.status == 301 || resp.status == 302) {
        set resp.http.location = req.http.location;
        return (deliver);
    }
}

you also need to write req.http.location properly. From what I understand, you want something like:

sub vcl_recv {
    if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
        set req.http.location = "/tickets + req.url + "-details";
        return(synth(301));
    }
}



回答2:


I think it would be better to do a regex substitution.

if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
  set req.url = regsub(req.url, "^/apple/", "/tickets/");
  error 701 req.url;
}


来源:https://stackoverflow.com/questions/58309227/how-to-add-redirect-exceptions-using-varnish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!