How to Redirect non-www to www URLs

Introduction

In order to redirect all non-www requests to your site to the www version (yourdomain.comto www.yourdomain.com), you should set the appropriate rewrite rule at the beginning of the .htaccess file in your public_html folder.
There are several benefits in doing that:

  • It will avoid duplicate content in Google.
  • It will avoid the possibility of split page rank and/or split link popularity (inbound links).

What you’ll need

Before you begin this guide you’ll need the following:

  • Access to Hosting control panel

Step 1 — Accessing File Manager

Login to your control panel and click on the File Manager in the Files section.

Navigate to public_html directory and edit your .htaccess file. In case a file named .htaccess does not exist, you will need to create it by right clicking your mouse and selecting New File option.

f you hosting platform is on cPanel, the process of entering File Manager is the same. However, there are some additional steps that you will need to take in order to view hidden files as .htaccess

You will get prompted with a pop-up menu asking what directory you would like to start in. To go directly to where your website files are stored make sure to check the Web Root and press Go button. Make sure that Show Hidden Files (dotfiles) option in File Manager is enabled.

You may check File Manager settings by clicking on Settings button inside your File Manager.

Step 2 — Editing .htaccess file

Enter your .htaccess by right-clicking and selecting Edit option.

If you are using cPanel, open your .htaccess file in Editor View.

Insert the following lines to your .htaccess file. Don’t forget to modify the code with your actual domain name instead of yourdomain.com.

Redirect non-www to www code:

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

After you edit the code, don’t forget to Save your changes.

Congratulations, you have successfully enabled a .htaccess rule that will redirect all visitors from non-www to www version of your website!

Conclusion

By finishing this tutorial you have learned how to redirect non-www URLs to www. From now on, when someone accesses http://yourdomain.com they will be redirected to http://www.yourdomain.com. Instead of using the cPanel method, many people find changing the .htaccess file via FTP or SSH an easy alternative.