0

我正在尝试将路径例如 www.something.com/apple/pie 重定向到 www.something.com/tickets/pie-details 但也有一些例外情况,例如 www.something.com/apple/helloworld 不会被重定向到 www .something.com/tickets/helloworld-details

这是我尝试过但不起作用的方法:

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

2 回答 2

1

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

举个例子(直接来自帖子):

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

您还需要req.http.location正确书写。据我了解,你想要这样的东西:

sub vcl_recv {
    if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
        set req.http.location = "/tickets + req.url + "-details";
        return(synth(301));
    }
}
于 2019-10-10T17:45:00.773 回答
0

我认为做一个正则表达式替换会更好。

if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
  set req.url = regsub(req.url, "^/apple/", "/tickets/");
  error 701 req.url;
}
于 2019-10-09T22:33:46.190 回答