Regex Nginx:通过单个内联更改将多个URL格式重定向到同一格式

Regex Nginx:通过单个内联更改将多个URL格式重定向到同一格式,regex,nginx,Regex,Nginx,我是nginx新手,需要设置一些重定向,我知道nginx中的重定向是基于正则表达式的,但是这也不是我的强项 我们正在启动最新的代码,以及如何重定向以下所有实例 https://uat1.lipsum.com/browse https://uat1.lipsum.com/popular uat1.lipsum.com/browse/[anything] uat1.lipsum.com/popular/[anything] 到 基本上,将所有出现的“浏览”和“流行”替换为“发现” 我试过几种

我是nginx新手,需要设置一些重定向,我知道nginx中的重定向是基于正则表达式的,但是这也不是我的强项

我们正在启动最新的代码,以及如何重定向以下所有实例

https://uat1.lipsum.com/browse
https://uat1.lipsum.com/popular
uat1.lipsum.com/browse/[anything]   
uat1.lipsum.com/popular/[anything]

基本上,将所有出现的“浏览”和“流行”替换为“发现”

我试过几种方法

我能想到的最好的格式是下面的格式,它可以正确地重定向,但其他格式不能

https://uat1.lipsum.com/browse
https://uat1.lipsum.com/popular
nginx.conf

location /browse/ {
    rewrite ^(.*)/browse/(.*)$ /discover/ permanent;
    }

    location /browse {
    rewrite ^(.*)/browse /discover permanent;
   }

 location /popular/ {
    rewrite ^/popular/ /discover/ permanent;
    }

    location /popular {
    rewrite ^/popular /discover permanent;
   }
}

使用反向引用的魔力。将正则表达式放入()中捕获匹配的表达式,然后使用$1、$2、$3等将提取第一个、第二个、第三个等捕获的文本

location /browse/ {
    rewrite ^/browse/(.*)$ /discover/$1 permanent;
    }

    location /browse {
    rewrite ^/browse$ /discover permanent;
   }

 location /popular/ {
    rewrite ^/popular/(.*)$ /discover/$1 permanent;
    }

    location /popular {
    rewrite ^/popular$ /discover permanent;
   }
}

有关更多信息,请参阅。

使用反向引用的魔力。将正则表达式放入()中捕获匹配的表达式,然后使用$1、$2、$3等将提取第一个、第二个、第三个等捕获的文本

location /browse/ {
    rewrite ^/browse/(.*)$ /discover/$1 permanent;
    }

    location /browse {
    rewrite ^/browse$ /discover permanent;
   }

 location /popular/ {
    rewrite ^/popular/(.*)$ /discover/$1 permanent;
    }

    location /popular {
    rewrite ^/popular$ /discover permanent;
   }
}

有关更多信息,请参阅。

非常感谢您的清晰解释和修复。它工作得很好!非常感谢您的清晰解释和修复。它工作得很好!