Apache
In the Web server configuration file, or the .htaccess file, append the following commands:
Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] or Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC] RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
Both options are acceptable.
A reverse redirect (from mydomain.com to www.mydomain.com) is carried out using commands:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.(.*) [NC] RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L] or Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
Both options are acceptable.
Restart the Apache Web server using the command:
apachectl restart or apache2ctl restart
Nginx
In the Nginx Web server configuration file, make the following changes.
Add section:
server { listen 80; server_name www.mydomain.com; rewrite ^ http://mydomain.com$request_uri? permanent; }
For a reverse redirect (from mydomain.com to www.mydomain.com), add the section:
server { listen 80; server_name mydomain.com; ..... }
For a reverse redirect (from mydomain.com to www.mydomain.com), add the section:
server { listen 80; server_name domain.com; rewrite ^ http://www.domain.com$request_uri? permanent; }
Add or modify the existing section with the master domain settings:
server { listen 80; server_name www.mydomain.com; ..... }
Restart the Nginx Web server using the command:
systemctl restart nginx