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://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!

Frequently Asked Questions

How to redirect non-WWW traffic to WWW using Nginx?

To redirect non-WWW traffic to WWW using Nginx, you can edit the Nginx configuration file and add a server block with a 301 redirect rule from non-WWW to WWW. This ensures that all traffic is directed to the preferred domain.

What is the importance of redirecting non-WWW to WWW traffic for SEO?

Redirecting non-WWW to WWW traffic is important for SEO as it helps consolidate your website's authority under one domain. Search engines like Google recommend setting a preferred domain to avoid duplicate content issues.

Where can I find the Nginx configuration file to set up redirects?

You can find the Nginx configuration file typically at /etc/nginx/conf.d/default.conf. This file contains the server blocks where you can configure redirects and other server settings.

Is it necessary to select a preferred domain in Google Webmaster before setting up redirects?

It is recommended to select a preferred domain in Google Webmaster before setting up redirects to ensure consistency in how search engines index your site. This helps in avoiding domain version conflicts.

Can Apache handle increased web traffic better than Nginx?

In some cases, Apache may struggle to handle increased web traffic compared to Nginx. Nginx is known for its efficient handling of concurrent connections and is often preferred for high-traffic websites.

What are the benefits of using Nginx over Apache for WordPress sites?

Nginx is preferred over Apache for WordPress sites due to its better performance under heavy loads. Nginx consumes fewer server resources and can handle more simultaneous connections efficiently.

How does setting a preferred domain in Google Webmaster affect SEO?

Setting a preferred domain in Google Webmaster can positively impact SEO by consolidating link equity and avoiding duplicate content issues. It helps search engines understand the canonical version of your site.

Why should you configure a 301 redirect when setting a preferred domain in Google Webmaster?

Configuring a 301 redirect when setting a preferred domain in Google Webmaster ensures that all traffic is redirected to the chosen domain consistently. This helps in maintaining the site's authority and rankings.