【问题标题】:How can I redirect non-www to www.domain.com and along with that also keep the m.domain.com / subdomain.domain.com same as it is in nginx?如何将非 www 重定向到 www.domain.com 并同时保持 m.domain.com / subdomain.domain.com 与 nginx 中的相同?
【发布时间】:2017-01-18 07:57:32
【问题描述】:

我用过这个:

服务器{
server_name "~^(?!www.).*" ;
返回 301 $scheme://www.$host$request_uri;
}

但这会重定向所有内容。我需要为此编写子域的异常。

【问题讨论】:

  • 如果您有一个与每个子域完全匹配的服务器块,则它应该优先于这个正则表达式块

标签: redirect nginx


【解决方案1】:

如果包含server_name "~^(?!www.).*" 的服务器块与m.domain.com 匹配,那么您显然没有另一个带有server_name m.domain.com 的服务器块。

您可以使用默认服务器块来执行重定向,而不是使用复杂的正则表达式将网站名称重定向到默认服务器块。

例如:

server {
    listen 80 default_server;
    return 301 http://www.domain.com/$request_uri;
}
server {
    listen 80;
    server_name www.domain.com m.domain.com subdomain.domain.com;
    ...
}

更多详情请见this document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2018-12-30
    • 2018-06-23
    • 2020-12-31
    相关资源
    最近更新 更多