This blog is now powered by WordPress and Nginx webserver. We started with the LAMP Stack (Linux, Apache, MySQL, and PHP) in the beginning but for some reasons, Apache couldn’t properly handle increased web traffic to this site without crashing almost nightly.

So we switched to Nginx. Now this blog is running smoothly without crashing under heavy loads using the same hardware resource.

Since switching to Nginx webserver, we’ve learned a lot and most of what we’ve learned have been documented on this blog. If you need help getting started with Nginx, search this site for more useful Nginx content.

In this blog post, I am going to show you how to 301 redirect your NON-WWW traffic to WWW so that they appear as one site. You see, some engines consider your NON-WWW and WWW traffic to be different.

For example, Google recommends that you set your preferred domain in Google Webmaster. You can either choose to redirect all WWW traffic to the bare domain (http://example.com) or NON-WWW traffic to (http://www.example.com).

If you setup a preferred domain in Google Webmaster, you should also configure a 301 redirect so that all traffic are redirect to your preferred domain. Using Nginx, it easy and this post is going to show you how.

To get started, first select the preferred domain in Google Webmaster.

 

301-redirect-nginx

 

When you’re done, sign in to your web host and browse to your Nginx directory and open the default site settings file. This file is usually installed at /etc/nginx/conf.d/default.conf.  It could be somewhere else with other Linux operating systems.

When you open the file, you should see the default Nginx setting for a default site. The default config comes with only one server block. A server block is the section of the file that starts with ( server { ) and ends with the opposite ( } ) tag.

Nginx site config file can host multiple server blocks.

So, to create a 301 redirect so that all traffic gets redirected to your WWW preferred domain, add a new server block at the top of the file and save. This block should look like this.

server {
        server_name _;
        return 301 $scheme://www.liberiangeek.net$request_uri;
}

 

The above redirect redirects or add WWW to all traffic.  To do the opposite, remove the www from the URL and save.

That’s how it done.

 

Enjoy!