How to Redirect non www to www on DigitalOcean/Ngnix/Ubontu/ServerPilot

Creating a redirect non www to www URL or www to non www could be both easy and difficult for digitalOcean/Nginx/Ubuntu/Serverpilot users. Here I am going to show you the easiest method to redirect them. To understand the total process you must have knowledge on URL redirection. If you already have any idea on URL redirection then start reading from How to redirect non www to www URL on digitalocean.

What is URL Redirection?

Redirection is a method of replacing one URL with another one. Basically, we use this redirection method to drive traffic directly from one URL to another one. We have to do this for different purposes. Suppose, you wrote a promotional content and you have back linked your content on many websites. You are receiving lots of traffic to that link but the promotional period has expired. As you are not getting benefit from that content you can easily delete that promotional content and can redirect your deleted link with other content that you may want more traffic.

You can easily redirect one sub URL to another sub URL under your domain name.  But it is quite difficult to redirect your non www to www or www to non www URL. You can redirect any sub URL using different plugins on WordPress Site. But you can’t redirect www to non www URL or non www to www URL. You must have a little coding knowledge to do it. There are 5-6 methods that you can change your website’s domain from non www to www URL or www to non www URL. I am going to use htaccess method.
Before we go ahead, let’s see the classification or types of redirection. It will help us to select the right way.

Types of URL Redirection

I am not going to teach you about the classification of URL redirection. I am just showing you types of URL redirection that are widely used and SEO friendly. Each of the redirection is marked by a response code.

302 Redirect = Found or Temporarily Moved (HTTP Version 1.0).
307 Redirect = is known as “Temporary Redirect” (HTTP Version 1.1).
301 Redirect = Permanently moved (redirect).

301 redirect is the most used redirection code. 301 Redirection is good for website’s SEO because it tells the search engine that the Original URL is no longer existed and has been permanently redirected to the new one. You can redirect your website from non www to www URL or www  to non www version using 301 redirection method. 301 redirection method is good for changing your website from old domain to new domain also. As 301 redirection method SEO friendly, it will not hamper your website’s SEO. So, we are using this method to change URL version from non www to www or non www to www. It is a kind of canonicalization. Read more about canonicalization.

Well I guess, you have some idea on redirection method now. So let’s move to the next level and see

How to Redirect non www to www URL on DigitalOcean /Nginx /Ubuntu (Vise-Versa)

DigitalOcean is a cloud base hosting provider and there are no CPanel on a cloud server. So it’s quite difficult to find htaccess. Many web owners are searching on google for DigitalOcean redirect non www to www on google. If you are using WordPress then stop worrying because you already use a plugin that can change your htaccess file. Can you guess it? Okay, I am telling the answer. It’s YOAST SEO plugin.


Method of  non www to www URL redirection

There are lots of ways to change htaccess files. I am going with the easiest way to change htaccss files. Almost all the website owner uses Yoast Seo Plugin. Its very easy to change htaccess file from Yoast Seo Plugin.  To change your htaccess files from yoast seo plugin follow the steps below. Also, you can go with the method that I used on the video (only if you are on serverpilot).
1.Login to your WP dashboard.

2.Go to Tools from Yoast SEO.

yoast seo to tools

3.Click on File Editor. You will find two boxes. One for editing robots.txt and another one for htaccess file
non-www url to www url

4.  After

# BEING WordPress add the codes below

# Start: non www to www url 
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# End: non www to www url

Hopefully you have been successful to redirect your non www to www url.

Redirect your www URL to non www URL

It is also like the previous one. You just need to place these codes

# Start: www to non www URL
RewriteEngine On
RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# End: www to non www URL 

 

These are the best ways that I know and shared them for whom are searching for digitalocean redirect non www to www or www to non www URL. I guess you can now easily change your desired version of your websites URL.

Caution: While editing codes on htaccess if you do anything wrong then your WordPress site may be destroyed. Then you have to remove the codes that destroyed your WordPress. See this video to delete unwanted codes from htaccss file

Conclusion :

If you are running a content-based website then do not use both versions non www and www of your domain. Because Google will find two same content in two different URL and you may get penalized for content duplication.Still now, if you have any problems then feel free to comment below.

4 Comments

  1. Jason January 7, 2017
    • Md. Mostafizur Rahman March 4, 2017
  2. niall March 3, 2017
    • Md. Mostafizur Rahman March 4, 2017

Leave a Reply