Update: This may be a better solution: Nginx redirect www

Introduction

It is not a good idea from the SEO point of view, to have duplicated content. If your content can be accessed from both www.yourdomain.com and yourdomain.com you will have duplicated content. This is true at least for Google and other search engines.

So, to avoid that, you need to forward all your www.yourdomain.com/something to yourdomain.com/something, of course you can do it the other way too.

Today we will see how to remove the www part on your URI, on Nginx, I like Nginx as it is really fast at serving static content, a lot faster and lighter than Apache.

Hands on

OK, time to work.

With Nginx, this is really easy I mean redirects.

Take the www out of your URI

if ($host = 'www.yourdomain.com' ) {
    rewrite  ^/(.*)$  http://yourdomain.com/$1  permanent;
}

This will take out the www, if you want to redirect anything.yourdomain.com to yourdomain.com you may use:

Take any prefix out of your URI

if ($host != 'yourdomain.com' ) {
    rewrite  ^/(.*)$  http://yourdomain.com/$1  permanent;
}

In the first example, we are telling Nginx, if the host is www.yourdomain.com take out the www from it. In the second example, we are saying if the host is not yourdomain.com make it yourdomain.com.