Nginx URL重写以在其后跟某些子文件夹时从URL中删除文件夹

时间:2019-10-31 12:32:35

标签: .htaccess nginx mod-rewrite url-rewriting friendly-url

升级网站后,我发现使用新版本上线后,由于新结构,网站URL的某些部分将不会重定向到图库,博客和文件。而且无法在CMS中对其进行修复。因此,我的目标是使用NGINX重定向。

我想知道你们中的任何人都知道NGINX重写技巧可以实现这种重定向吗?

website.com/forums/blogs/ into  website.com/blogs/
website.com/forums/gallery/ into website.com/gallery/
website.com/forums/files/ into website.com/files/

我实际上只需要从URL删除一部分论坛,并且仅当该地址用于论坛+博客/画廊/文件时才需要。不想失去Google流量。

例如     website.com/forums/blogs/entry123/my-dog/重定向到     website.com/blogs/entry123/my-dog /

但是     website.com/forums/topic/my-dog /

由于下面的子文件夹既不是博客,也不是画廊或文件,因此可以像以前一样单独工作。

我曾经需要在Apache上使用它,但是这个方法可以工作,但是在Nginx上我不知道。

RewriteRule ^forums/(blogs|gallery|files)/(.*)$ /$1/$2 [L,R=301]

1 个答案:

答案 0 :(得分:0)

您可以尝试类似

rewrite ^/forums/(blogs|gallery|files)/(.*)$ /$1/$2;

请注意,rewrite指令接受一些标志,其含义取决于它放置的位置(位于serverlocation块中)。详细文档为here