Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Redirect www to non-www Website Or Vice Versa | StackCoder


Redirect www to a non-www Website Or Vice Versa


14th April 2020 4 mins read
Share On     Share On WhatsApp     Share On LinkedIn


In today's fast-moving website competition SEO is the game-changer of your website. One of the problem what an SEO faces is multiple links to your domain, for example, www.stackcoder.in and stackcoder.in ie redirect www or a non-www website or vice versa. 

If your looking out for LEMP installation then please do check my another article on it How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu


Problem


The major impact for SEO is when user may come from www or non-www link of your website. And if your web server is not capable to handle those and start serving on the same requested links, then your SEO score is totally divided between your 2 domains links.


Solution using NGINX web server


You need to redirect all your users website URL requests from your web server ( NGINX or APACHE ) to single point those ie either to www.stackcoder.in or stackcoder.in with 301 HTTP status redirect.

301 redirect is a permanent redirect which redirects users & search engines like Google, Yahoo, DuckDuck Go, etc. But we need to tell them where does this redirect to.

If you like to learn NGINX caching then I have already written article on it How To Cache Static Files With NGINX Server.


Go to your website NGINX server configuration which will be mostly in /etc/nginx/sites-available/* and mine is /etc/nginx/sites-available/stackcoder.in which will basically have the following configuration. I have striped few code lines but structure remains the same for you guys


server {
  listen 443 ssl http2;
  root /var/www/html/public;
  index index.php index.html;
  server_name stackcoder.in www.stackcoder.in;

  location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
  }

  #SSL CERTIFICATE FILES
}

server {
  listen 80;
  server_name stackcoder.in www.stackcoder.in;
  return 301 https://$host$request_uri;
}

Option 1 - Redirect all the requests from www.stackcoder.in to stackcoder.in


In the following configuration file NGINX am listening on 443 port on HTTPS for website URL www.stackcoder.in and redirecting to permanently with 301 HTTP status code to non-www website ie stackcoder.in


server {
  listen    443 ssl http2;
  #Listen on https for https://www.stackcoder.in
  server_name www.stackcoder.in;
  #Redirect to non www URL ie https://stackcoderin
  return    301 https://stackcoder.in$request_uri;
}


With the above change, your overall NGINX configuration file must look like the following


server {
    root /var/www/html/public;
    index index.php index.html;
	  server_name stackcoder.in;

    location / {
		  try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    listen 443 ssl http2;
    #SSL CERTIFICATE FILES
}
server {
  listen    443 ssl http2;
  server_name www.stackcoder.in;
  return    301 https://stackcoder.in$request_uri;
}
server {
  listen 80;
  server_name stackcoder.in www.stackcoder.in;
  return 301 https://$host$request_uri;
}

Option 2 - Redirect all the requests from stackcoder.in to www.stackcoder.in


This is similar to Option 1 with minor change in the configuration syntax

Here NGINX is listening on 443 port on HTTPS for website URL stackcoder.in and redirecting to permanently with 301 HTTP status code to non-www website ie www.stackcoder.in


server {
  listen    443 ssl http2;
  #Listen on https for https://www.stackcoder.in
  server_name stackcoder.in;
  #Redirect to non www URL ie https://stackcoderin
  return    301 https://www.stackcoder.in$request_uri;
}


With the above change, your overall NGINX configuration file must look like the following


server {
    root /var/www/html/public;
    index index.php index.html;
	  server_name www.stackcoder.in;

    location / {
		  try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    listen 443 ssl http2;
    #SSL CERTIFICATE FILES
}
server {
  listen    443 ssl http2;
  server_name stackcoder.in;
  return    301 https://www.stackcoder.in$request_uri;
}
server {
  listen 80;
  server_name stackcoder.in www.stackcoder.in;
  return 301 https://$host$request_uri;
}

RESTART your NGINX server


Once you opt for either Option 1 or Option 2 make sure to restart your NGINX web server to take the changes into effect. Else the configuration that you have added wont work.

First check if the NGINX added code to configuration file is proper or not by the following command


sudo nginx -t


You must be able to see similar output


nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


Now reload the NGINX server configuration file with the following


sudo service nginx reload

Conclusion


Now you have learned how to setup NGINX redirects. I have written other articles which might be very interesting for you How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu where you will learn about step by step installation of LEMP stack.

If your looking our to securing you server then make sure not to miss out this tutorial How To Do Basic Server Security Setup For Ubuntu / Linux


Hope it helped you. Happy Coding :)






Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing