简体   繁体   English

Nginx-仅将主域而不是子域重定向到WWW

[英]Nginx - Redirect only main domain and not subdomain to WWW

How can I setup a redirection in Nginx conf to only redirect the main domain and not subdomain to WWW using $host or $server_name ? 如何在Nginx conf中设置重定向,以仅使用$host$server_name将主域而不是子域重定向到WWW?

Any help will be appreciated. 任何帮助将不胜感激。

server {
    server_name your_domain_without_www;  
    location / {
         rewrite   ^(.*)$ http://your_domain_with_www$1 permanent;
    }
}

server {
     server_name *.your_domain_without_www;
     ...
}

You could also split the second server section into 2 separate ones if you need to handle www.your_domain and other subdomains of your domain differently. 如果您需要不同地处理www.your_domain和域的其他子域,也可以将第二个服务器部分分成两个单独的部分。

使用.htacess文件重定向网址,这将解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM