How To Permanent Redirect (301 Redirect) NON WWW to WWW and viceversa on apache, nginx and lighttpd

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:
DO NOT forget the dot (files beginning with a dot are considered to be “hidden” by the UNIX/Linux Systems.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.tld$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.tld/$1 [R=301,L]

How to permanent redirect (301 redirect) WWW to NON WWW using .htaccess directives:
Create a file named .htaccess and append the following content:
DO NOT forget the dot (files beginning with a dot are considered to be “hidden” by the UNIX/Linux Systems.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.tld$ [NC]
RewriteRule ^(.*)$ http://yourdomain.tld/$1 [R=301,L]

2. Lighttpd

If you are using lighttpd then these are the directives you need to add to lighttpd.conf, or to the file you declared your virtual hosts in:

How to permanent redirect (301 redirect) NON WWW to WWW using lighttpd:

$HTTP["host"] =~ "^yourdomain\.tld$" {
  url.redirect = ( "^/(.*)" => "http://www.yourdomain.tld/$1" )
}

How to permanent redirect (301 redirect) WWW to NON WWW using lighttpd:

$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect = ( "^/(.*)" => "http://%1/$1" )
}

3. NGINX

Well, this is my favorite and I consider it to be the simplest so if you want to permanent redirect (301 redirect) NON WWW to WWW insert the following code in your nginx configuration file:

server {
  listen 80;
  server_name www.yourdomain.tld;
  rewrite ^/(.*) http://yourdomain.tld/$1 permanent;
}

Otherwise, if you want to permanent redirect (301 redirect) WWW to NON WWW in nginx, use this code in your nginx.conf file:

server {
  listen 80;
  server_name yourdomain.tld;
  rewrite ^/(.*) http://www.yourdomain.tld/$1 permanent;
}

Please post in the comments section any question.

Leave a Reply

Your email address will not be published. Required fields are marked *