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.

Updated on 15th April 2018 to make better use of “checking” i.e. != (not equal)

 

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 != "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 != "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 != "www.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 != "www.vestacp.com") { return 301 http://www.vestacp.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>