07/29/2017

Nginx www 重定向

裸域名 example.com -> www.example.com and vise-versa 的 Nginx 实现方法。

摘自Gist

Nginx WWW Redirection

Redirect non-www to www

For Single Domain:

server {
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

For All Domains:

server {
    server_name "~^(?!www\.).*" ;
    return 301 $scheme://www.$host$request_uri;
}

Redirect www to non-www:

For Single Domain:

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

For All Domains:

server {
    server_name "~^www\.(.*)$";
    return 301 $scheme://$1$request_uri;
}

本文链接:https://blog.whe.me/post/nginx-www-redirection.html

-- EOF --

Comments

评论加载中...

注:如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理。