WWW and Non WWW version in Nginx redirects

WWW and non WWW versions are treated as 2 separate sites indeed, its recommended to choose a version and redirect other to maintain uniformity and take full advantage of URL's

Advert

It's common to see domains working on WWW and non-WWW versions and is indicated as duplicate page or in other form by most website monitoring tools. It does bring in SEO issues and more.

The root cause for the problem is with server configuration, in Apache once a domain is configured server accepts and serves content for both WWW and non-WWW versions out of the box, it's well and good as it serves your users hitting either version.

Nginx doesn't do the same, it treats both as separate version which all servers do but it doesn't serve both same, if configuration exist for WWW version Nginx will only server the contents if user requests WWW version. So it indeed Important to have a redirect in Nginx.

Apache this can be achieved by adding a few rules to .htacces but Nginx needs a little extra edits in Configuration.

First lets get started by creating a configuration file for both WWW and non WWW version of the domain in /etc/nginx/sites-available/, it should have the following files ( exampe.com = yourdomain ) :

  1. example.com
  2. www.example.com

Install the site rules on the file of which the version you like to have. On the other Install the 301 permanent redirect redirect rules, Redirect rules for WWW to non-WWW is :

WWW to non-WWW

server {
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
}
non-WWW to WWW

server {
        server_name example.com;
        return 301 $scheme://www.example.com$request_uri;
}

Once configurations are done, link the file to an entry with file name in /etc/nginx/sites-enabled/ directory and restart the nginx in-oder for the configurations to take effect.

Server Setting for All domains

To install this rule on all sites hosted you can have the rules set on the global configuration file, with a rule to select domain too.

Redirect non-www to WWW

server {
        server_name "~^(?!www\.).*" ;
        return 301 $scheme://www.$host$request_uri;
}

In both case we are creating a new domain configuration and initiate the redirect, this is handy in one view as it prevents the site from existing on both versions but not good in other.

Comments

Wow ! you have someting to tell us. That's great! Please keep in mind that comments are moderated, we employ rel="nofollow" for links, avoid using a spammy word or a domain in name field, it might end up as a Spam. Thanks for reading.

Last 5 Articles

All Articles >

  1. Domain Authority

     
  2. Public Endpoint in Django Rest Framework

     
  3. 4 Ways to Increase Conversion Rates using Videos

     
  4. Getting the best customer experience analytics solutions

     
  5. How to Remove an App from Django Project

     

News Letter

Subscribe to our email newsletter for useful tips and valuable resources, sent out every new article release.