Nginx重定向规则无效

Rob*_*Rob 6 redirect nginx url-rewriting

尝试做一个简单的重定向:

rewrite https://url.example.com(.*) https://example.com/plugins/url permanent;
Run Code Online (Sandbox Code Playgroud)

任何时候url.example.com被击中,我都希望它重定向到该特定路径。

编辑:

将尝试更好地解释这一点,因为我正尝试从另一个域重定向到特定域。

server {
    server_name example.com plugin.example.com;
    root /home/www/example.com/public;
}
Run Code Online (Sandbox Code Playgroud)

我看到了location用于重定向的内容,例如:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
Run Code Online (Sandbox Code Playgroud)

但不确定在我的情况下如何使用它,即更改plugin.example.comexample.com/plugin

例如:

http://plugin.example.com
https://plugin.example.com
https://plugin.example.com/blah
https://plugin.example.com/blah/more
Run Code Online (Sandbox Code Playgroud)

所有这些都应重定向到:

https://example.com/plugin
Run Code Online (Sandbox Code Playgroud)

cns*_*nst 1

从子域重定向到主站点上的子文件夹

\n

您是否需要从子域重定向到主站点上的子文件夹?

\n
    \n
  • 这最好通过server具有适当server_name规范的单独上下文来完成。

    \n
  • \n
  • 另外,您也可以通过if针对$host.

    \n
  • \n
  • 正如其他地方已经指出的,rewrite指令的运作基于$uri,它不包含主机名。

    \n
  • \n
\n
\n

server_name基于匹配(推荐):

\n

使用有限数量的主机名进行硬编码重定向(推荐):

\n
server {\n    server_name     plugin.example.com;\n    return  301     $scheme://example.com/plugin$request_uri;\n}\nserver {\n    server_name     about.example.com;\n    return  301     $scheme://example.com/about$request_uri;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

从任何子域到主域的基于正则表达式的重定向:

\n
server {\n    server_name     ~^(?:www\\.)?(?<subdomain>.*)\\.example\\.com$;\n    return  301     $scheme://example.com/$subdomain$request_uri;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

基于正则表达式的从有限数量的子域到主域的重定向:

\n
server {\n    server_name     ~^(?:www\\.)?(?<subdomain>plugin|about)\\.example\\.com$;\n    return  301     $scheme://example.com/$subdomain$request_uri;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n
\n

if-基于:

\n

使用硬编码主机名的基于 if 语句的重定向:

\n
server {\n    server_name     .example.com;\n    \xe2\x80\xa6\n    if ($host = plugin.example.com) {\n        return  301     $scheme://example.com/plugin$request_uri;\n    }\n    if ($host = about.example.com) {\n        return  301     $scheme://example.com/about$request_uri;\n    }\n    \xe2\x80\xa6\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

基于 If 语句的重定向以及基于正则表达式的匹配:

\n
server {\n    server_name     .example.com;\n    \xe2\x80\xa6\n    if ($host ~ ^(?:www\\.)?(?<subdomain>plugin|about)\\.example\\.com$) {\n        return  301     $scheme://example.com/$subdomain$request_uri;\n    }\n    \xe2\x80\xa6\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

请参阅http://nginx.org/r/server_name了解有关哪个选项最适合您的更多讨论。

\n