This is my favorite and I think the more simple  if you want permanent redirect  from NON WWW to WWW insert the following code in the nginx configuration file nginx.conf :

server {
  listen 80;
  server_name www.debian-tutorials.com;
  rewrite ^/(.*)$ http://debian-tutorials.com/$1 permanent;
}
Permanent redirect from WWW to NON WWW in nginx, use this code in your nginx.conf :
server {
  listen 80;
  server_name debian-tutorials.com;
  rewrite ^/(.*)$ http://www.debian-tutorials.com/$1 permanent;
}

Enjoy!

Apache2 is one of the most widely used HTTP server has a lot of features and can be customized in many ways.

In fact, to this web server, there are basically two ways you can do this, using .htaccess directives or using a file httpd.conf. .

Some people prefer to use. htaccess files, since they reside in the website and can be modified by the site owner, which is not the case for the daemon configuration file:

 

Permanent redirect  NON WWW to WWW using apache2 server configuration:
ServerName debian-tutorials.com
RedirectMatch permanent ^/(.*) http://www.debian-tutorials.com/$1
DocumentRoot /path/to/your/site/files/
ServerName www.debian-tutorials.com
Page 1 of 11