【发布时间】:2023-03-21 00:09:01
【问题描述】:
我正在尝试设置 Nginx 重定向,它将任何 URL 从几个旧论坛设置重写到新论坛。旧版论坛从子文件夹运行,而当前论坛从同一站点的子域运行。
因此,例如,我希望将对 site.com/ask 的 ANY 请求重定向到 forum.site.com 的首页。由于我正在处理 3 个旧论坛,因此我尝试像这样设置嵌套重定向:
location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~ask/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
location ~ ^/\~forum/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
location ~ ^/\~qa/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
}
根据上述规则,只有第一个有效且部分有效。例如,对 site.com/ask 的请求被重定向到 forum.site.com,这很好,但是对 site.com/ask/what-is-this 的任何请求都会转到 forum.site.com/ 404.
对 site.com/forum 和 site.com/qa 的请求根本不起作用。
我确信有一种更简单的方法可以做到这一点,但我不想花几天时间去弄清楚。
欢迎并感谢您的意见。
编辑:
上面的代码没有得到任何结果,我将其简化为:
location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~(qa|forum|ask)/(.*)$ {
rewrite ^/~(qa|forum|ask)/(.*)$ http://forum.site.com$1 permanent;
}
}
但结果还是一样。有什么想法吗?
【问题讨论】:
-
在你的评论中你提到了
site.com/ask,但你的代码是site.com/~ask/。哪个是对的? -
也许这就是问题所在。它是 site.com/ask、site.com/forum 和 site.com/qa。有没有更好更简洁的方法?
-
添加了对代码的修改。