在重定向整个域/全部捕获之前,在Nginx中重定向301特定页面 [英] 301 Redirect Specific Pages in Nginx BEFORE Redirecting Entire Domain/Catch All

查看:95
本文介绍了在重定向整个域/全部捕获之前,在Nginx中重定向301特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nginx中有成千上万的页面到301重定向.我可以使用

I have thousands of pages to 301 redirect in nginx. I can achieve this using

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

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

However, there is approximately 6 pages that I would like to redirect to a changed slug/path on the new domain, eg

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

我已经尝试过了,但是看不出如何首先重定向这6个,然后将全部捕获"规则应用于其他所有内容.

I have tried but can't see to work out how to redirect these 6 first, specifically, and then have the catch all rule apply to everything else.

此刻,我仅使用全部捕获功能在旧域上进行重定向,然后在新域上使用301将6个帖子更改为新路径(这6个帖子共2个重定向).

At the moment I am redirecting on the old domain using the catch all only, and then have 301s on the new domain changing the 6 posts to the new paths (2 redirects total for these 6 posts).

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

I would like to achieve the above not only to reduce the redirects to 1 for these 6 pages, but also because I want to redirect one of the 6 pages to a new path on the new domain, but it's old path still exists as a (new) page on the new domain too (therefore I need to redirect it on the old domain, not on the new one).

推荐答案

rewrite模块按顺序执行,因此整个过程可以单独使用rewritereturn指令完成,而无需将它们包装在location块中.例如:

The directives of the rewrite module are executed in order, so the entire process can be accomplished with rewrite and return directives alone, without wrapping them within location blocks. For example:

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块内.例如:

If you wrap the rewrite statements within location blocks, then the return statement must also be wrapped within a location block. For example:

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

这篇关于在重定向整个域/全部捕获之前,在Nginx中重定向301特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆