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

1. Log into your cPanel. 

2. Find the Files category and click on the File Manager icon.

3. Click Settings at the top right corner.

4. Choose the document root 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 '' with your actual domain name. NOTE: do not place both selections of code in the file as it will cause an error. 

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

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

7. Now, 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.
  • 124 Users Found This Useful
Was this answer helpful?

Related Articles

 How to deal with Git on our shared servers

1. OverviewGit is a distributed version control system. While it may sound confusing, it helps to...

 Restricting IP addresses access to subdomain via .htaccess file

In cPanel-> under Subdomains section you can create the subdomain for your domain.   After...