首页 > 解决方案 > nginx 根据 uri 中的参数 id 重定向

问题描述

需要根据 personId uri 参数进行重定向。

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

需要对每个 personId 值执行此操作,所以基本上如果请求是:https://server.com/de/prints/?personId=2 将其重定向到 https://anotherserver.com/dir1/dir2/dir3/dir4/ dir5/the-result?statusId=1&personId=2

我尝试了以下方法,但它不起作用:

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

标签: regexnginxredirectlocation

解决方案


为了$1在您的 URL 中使用,您需要使用括号(捕获组)捕获原始 URL 中人员的 ID。

类似的东西.*[&?]personId=([0-9]+)而不是^.*$

这是一个演示:https ://regex101.com/r/yms4Oi/1


推荐阅读