Force www in .htaccess

force www hero image

Many customers at some point request to either force www or non-www version of their site to display in their visitor’s browser. For example, you can have www.example.com or simply example.com display in an address bar. While this difference won’t affect how visitors find your site, it can be a matter of aesthetic preference. 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! 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.

Trust your website to an industry leader in website hosting. InMotion Hosting offers secure web hosting with 99.99% uptime, 24/7/365 technical support, and 100% money-back guarantee.
InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!