301 在重定向整个 Domain/Catch 全部之前重定向 Nginx 中的特定页面

301 Redirect Specific Pages in Nginx BEFORE Redirecting Entire Domain/Catch All

我有数千个页面要在 nginx 中进行 301 重定向。我可以使用

来实现
return 301 https://www.newdomain.com$request_uri;

但是,我想将大约 6 个页面重定向到新域上已更改的 slug/path,例如

   location /old-path/ {
   rewrite ^ https://www.newdomain.com/newpath/ permanent;
   }

我试过但看不出如何先重定向这 6 个,具体地说,然后让捕获所有规则应用于其他所有内容。

目前我只使用 catch all 在旧域上重定向,然后在新域上使用 301s 将 6 个帖子更改为新路径(这 6 个帖子总共重定向 2 次)。

我想实现上述不仅是为了将这 6 个页面的重定向减少到 1,而且因为我想将这 6 个页面之一重定向到新域上的新路径,但它是旧路径在新域上仍然作为(新)页面存在(因此我需要将它重定向到旧域,而不是新域)。

rewrite module的指令是按顺序执行的,所以整个过程可以单独使用rewritereturn指令来完成,而不用将它们包装在location中块。例如:

server {
    ...
    rewrite ^/old-path/ https://www.newdomain.com/newpath/ permanent;
    rewrite ^/...       https://www.newdomain.com/.../     permanent;
    return 301          https://www.newdomain.com$request_uri;
}

如果将 rewrite 语句包装在 location 块中,则 return 语句也必须包装在 location 块中。例如:

location / {
    return 301 https://www.newdomain.com$request_uri;
}
location /old-path/ {
    rewrite ^ https://www.newdomain.com/newpath/ permanent;
}