Many customers request to force their site's www or non-www version to display in their visitor’s browser. For example, you can have www.example.com or example.com displayed in an address bar. While this difference won’t affect how visitors find your site, it can be a matter of aesthetic preference. 

SEO recommendations suggest that it is best to force your domain to either the www, or non-www so that your website is treated by search engines as a single domain. Both www. and non-www are separate entities, as far as Google Search is concerned, and therefore you may be penalized for having 'duplicates' if this is not setup correctly for your domains.

This article will guide you through how to force www or non-www in your .htaccess file. You can access the .htaccess file through the cPanel File Manager. 

Force www or non-www in your .htaccess file in cPanel

  1. First, log into your cPanel.
  2. Once logged into cPanel, Find the Files category and click on the File Manager icon.
  3. Next, click Settings at the top right corner.
  4. In the Settings Menu, choose the document root (or folder) for the domain you are working with and be sure the checkbox next to Show Hidden Files is checked. Click the Save button.
  5. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option.
  6. You are now in the text editor. Place either selection of code in the file and click on the Save Changes button at the top right corner of the screen. Be sure to replace ‘example.com‘ with your actual domain name. NOTE: do not place both selections of code in the file as it will cause an error. Below are examples of code that you can use to implement the desired change to the website.
Force www
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]

Force non-www

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Congratulations, you have successfully forced www or non-www! When you type in your domain name with either www in front or not, it should display as you have set it in the .htaccess file.