Generic htaccess redirect www to non-www and non-www to www with .htaccess file

If you like to redirect to This is the generic method without specifying the domain name

put this code into .htaccess file

For both http and https:

www to non-www for both http and https

non-www to www for both http and https

With httpd.conf file, use this