Redirect WWW to non-WWW in NGINX with VestaCP

This very quick and easy solution will help you redirect WWW to non-WWW in NGINX with VestaCP.

Create a new file (if you already have it, place the next bit of code inside it) at /home/USER/conf/web/nginx.vestacp.com.conf and add the following code:

if ($host = "www.vestacp.com") { return 301 https://vestacp.com$request_uri; }

Where vestacp.com is replace with the domain name. Also take note, this snippet of code will redirect the user to the SECURE version. If you need the insecure version, use the following code:

if ($host = "www.vestacp.com") { return 301 http://vestacp.com$request_uri; }

 

Redirect non-WWW to WWW

Similarly if you want to redirect all secure traffic to non secure then use this code instead:

if ($host = "vestacp.com") { return 301 https://www.vestacp.com$request_uri; }

Where vestacp.com is, replace with the domain name. Also take note, this snippet of code will redirect the user to the SECURE version. If you need the insecure version, use the following code:

if ($host = "vestacp.com") { return 301 http://www.vestacp.com$request_uri; }