Force NGNIX www to non-www or redirect https:// traffic

Updated 26 December 2017

Here is an example how to force no-www domain on Servepilot VPS servers with Nginx. Create /etc/nginx-sp/vhosts.d/servermeister.d/0www_redirect.conf with code that will force all www pages 301 redirect to a naked no-www domain.

Code:

if ($host ~* www.servermeister.com$) { return 301 http://www.servermeister.nl$request\_uri; }

restart nginx with the command

Code:

sudo service nginx-sp restart

If you would like to force all no-www pages to www.

Code:

if ($host ~* techcat.ca$) { return 301 http://www.servermeister.nl$request\_uri; }

More info on Nginx redirects
https://serverpilot.io/community/articles/how-to-redirect-based-on-the-requested-domain-name.html