我如何在 Nginx 中匹配以下内容

How can I match the following in Nginx

亲爱的 Whosebug 用户,

我一直在尝试让两个简单的东西起作用,但我只能让其中的一个起作用。

所以我有 nginx 服务器块,如下所示

server {

        listen 443 default;
        server_name secure.<domain>.com;

        if ($host = $server_name) {
        rewrite ^(.*) https://secure.<domain2>.com$request_uri?systpl=<template> permanent;

}

我也试过,因为我知道 IF 是邪恶的我试过类似

return 301 https://secure.<domain2>.com$request_uri?systpl=<template>;

当然,我禁用了上面的 if 语句,这没问题,所以当我们转到

https://secure.domain.com => https://secure.domain2.com/?systpl=template

这很好用,当你输入 PHP 结尾时,例如

https://secure.domain.com/cart.php => https://secure.domain2.com/cart.php?systpl=template

这对 IF 和简单 return 都有效,我想我需要在不同的服务器块或语句中获取另一个,但这就是问题所在

现在如果你转到

https://secure.domain.com/cart.php?id=7 => https://secure.domain2.com/cart.php?id=7?systpl=template

这当然行不通,因为它必须是 &systpl 因为它不以 .php[=54 结尾=],我怎样才能做到这一点,所以它附加 &systpl 如果它不以 .php 结尾并以字符串结尾,如果如果它以 .php

结尾,则不要附加 ?systpl

我希望有人知道这是如何完成的,因为我已经使用各种搜索字符串抓取了 google 结果的前 6 页。

如果您要添加参数,您可能应该避免使用 $request_uri,而是使用 $uri$args

return 语句可以重写为:

return 301 https://example.com$uri?systpl=template&$args;

rewrite 指令默认附加参数,所以这应该有效:

rewrite ^ https://example.com$uri?systpl=template permanent;

但由于它与 return 语句相同,因此首选 return 语句。有关详细信息,请参阅 this document