How to force www or non-www in your .htaccess file in cPanel

This guide will show you how to force your website to either use www or non-www when visitors visit your website in their web browser. For example: www.yourdomain.com or yourdomain.com. 

1. Log into your cPanel Account

2. Locate the "Files" category and click on the "File Manager" icon.

3. Click on "Settings" in the top right hand corner

4. Make sure the document root for the domain name you have been working on is set correctly. Then make sure the box for "Show Hidden Files" is checked. It needs to have a tick in the box. Then click on the "Save" button.

5. Locate the .htaccess file and right click on it - Then select the "Edit" option. 

6. You should now be in the text editor - You need to choose which code you want to use such as the Force www or non-www. Place either selection of code in the .htaccess file and click "Save Changes". Make sure you replace yourdomain.com with your actual domain name. 

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

7. Once you have done the above, the change should now be applied and when you visit your site it should either take you to www.yourdomain.com or yourdomain.com depending on what you have changed. 

If you are having issues with the above or need further assistance. Please raise a support ticket through the clients portal. 

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to change the default index page in .htaccess

The web server will look for specifically named files as the first page of your website when you...