Since I’ve met a lot of people having issues redirecting www to non-www, using 301 redirect to avoid duplicate content on google, I decided it would be a nice thing to post the best methods used for some of the most used http servers.
1. Apache
Apache is one of the most used http servers, has a lot of features and can be customized in so many ways. Actually, for this web server, there basically 2 ways you can do this, using httpd.conf directives or using a .htaccess file. Some people prefer using .htaccess files, because these reside in the website’s directory and can be modified by the website owner, which is not the case for the daemon configuration file (httpd.conf). So I will present both methods:
How to permanent redirect (301 redirect) NON WWW to WWW using apache server configuration directives:
ServerName yourdomain.tld
RedirectMatch permanent ^/(.*) http://www.yourdomain.tld/$1
DocumentRoot /path/to/your/site
ServerName www.yourdomain.tld
How to permanent redirect (301 redirect) WWW to NON WWW using apache server configuration directives:
ServerName www.yourdomain.tld
RedirectMatch permanent ^/(.*) http://yourdomain.tld/$1
DocumentRoot /path/to/your/site
ServerName yourdomain.tld
How to permanent redirect (301 redirect) NON WWW to WWW using .htaccess directives:
Create a file named .htaccess and append the following content:
Read more