How to force www or non-www in htaccess
Written by Scott MitchellThe .htaccess file is a hidden text file within your hosting account. It is designed so that you can modify it to change the behavior of your website that normally would take higher access beyond your account. It acts as a liason between your domain and the server and can perform many functions.
Many customers at some point request to force either the www or non-www version of their site to display in their visitor's browser. For example always have www.domain.com or simply domain.com display in their visitor's web browser. There is a common thought that forcing one format is better for search engine optimization. This article will guide you through how to perform this action on your .htaccess in the cPanel for your primary domain.
How to force www or non-www in your .htaccess file in cPanel
- Log into your cPanel.
- Find the Files category and click on the File Manager icon.

- A popup box will appear. For the primary domain, click on the Web Root radio button. For addon domains, click on the dropdown and find your desired addon domain name. Be sure the checkbox next to Show Hidden Files is checked. Click the Go button to enter the File Manager.

- You should now be in the root folder of the domain you chose. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option. If you get a popup box, simply find and click the Edit button in the lower right corner to continue to the editor.

- 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.
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] - 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.
Latest Questions
Need more Help?
Search
Ask the Community!
Current Customers
| Chat: | Click to Chat Now | E-mail: | support@InMotionHosting.com |
|---|---|---|---|
| Call: | 888-321-HOST (4678) | Ticket: | Submit a Support Ticket |

