Nginx 301 转向 Redirect non-www to www

现在我也随着大流。。很少使用cpanel了。。。开始投入nginx 的怀抱了。。

那么以后常见的问题就是,如何做301转向,把non-www转向到www, 或者把www转向到non-www.

刚才仔细的看了一下目前网上的做法,大部分都是被nginx 官方不推荐的。。。

在这里做个记录,以后方便我查找

Redirect non-www to www:

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

 

Redirect www to non-www:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.