Let’s say you want to redirect users from the www sub-domain of your website to direct access via the non-sub-domain url. Nginx makes it really easy to do.
Just add this to your server{} block:
if ($host != 'your_domain.com' ) { rewrite ^/(.*)$ http://your_domain.com/$1 permanent; }
This actually will redirect any sub-domain to the non-sub-domain url.
But what if, like Less Accounting, your site has user accounts for sub-domains or you have other valid sub-domains, but you still want to get users away from www?
Just add this to your server{} block:
if ($host = 'www.your_domain.com' ) { rewrite ^/(.*)$ http://your_domain.com/$1 permanent; }
参考: http://b.lesseverything.com/2008/4/9/redirect-from-www-to-non-www-using-nginx