Web Development

Having different URLs can be bad for your website in terms of SEO. Most people recommend having one version of your URL. Here’s how to redirect from WWW to non-WWW URLs in NGINX.

Redirect from WWW to non-WWW version

Make sure you add a new server block with the following content. Just change website to your domain name.

server {
  server_name www.website.com;
  return 301 http://website.com$request_uri;

If you want it the other way around, add the server block below.

Redirect from non-WWW to WWW version

server {
  server_name website.com;
  return 301 http://www.website.com$request_uri;

You can now use this on your website. Take note that a 301 redirect is a permanent one and it is the one you should use.