简体   繁体   English

nginx根据来自uri的参数id重定向

[英]nginx redirect based on parameter id from uri

need to redirect based on personId uri parameter.需要根据 personId uri 参数重定向。

Redirect https://server.com/de/prints/?personId=1 to https://anotherserver.com/dir1/dir2/dir3/dir4/dir5/the-result?statusId=1&personId=1https://server.com/de/prints/?personId=1重定向到https://anotherserver.com/dir1/dir2/dir3/dir4/dir5/the-result?statusId=1&personId=1

Need to do that for every personId value, so basically if request is: https://server.com/de/prints/?personId=2 redirect it to https://anotherserver.com/dir1/dir2/dir3/dir4/dir5/the-result?statusId=1&personId=2需要为每个 personId 值执行此操作,因此基本上如果请求是: https://server.com/de/prints/?personId=2将其重定向到https://anotherserver.com/dir1/dir2/dir3/dir4/ dir5/the-result?statusId=1&personId=2

I've tried the following, but it's not working:我已经尝试了以下方法,但它不起作用:

location ~* ^/de/prints? {
    if ($args ~* "personId=*") {
      rewrite ^.*$ https://anotherserver.com/dir1/dir2/dir3/dir4/dir5/the-result?statusId=1&personId=$1 redirect;
    }
}

In order to use $1 in your URL you need to capture the Id of the person in the original URL with parentheses (capturing group).为了在您的 URL 中使用$1 ,您需要使用括号(捕获组)捕获原始 URL 中人员的 Id。

Something like .*[&?]personId=([0-9]+) instead of ^.*$.*[&?]personId=([0-9]+)而不是^.*$

Here is a demo: https://regex101.com/r/yms4Oi/1这是一个演示: https : //regex101.com/r/yms4Oi/1

I managed to make it work using the following:我设法使用以下方法使其工作:

location ~ /de/prints/ {
         if ($args ~* "personId=(.*)") {
             return 302 https://anotherserver.com/dir1/dir2/dir3/dir4/dir5/the-result?statusId=1&personId=$1;
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM