How to force www or non-www in htaccess

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, you can have www.example.com or simply example.com display in an address bar. 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 in your .htaccess file. You can access the .htaccess file through the cPanel File Manager.

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.
    cpanel
  3. Click Settings at the top right corner.
  4. cpanel
  5. 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.
    file-manager-show-hidden
  6. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option.
    file-manager-htaccess
  7. 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]
  8. 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.

Post a Comment

0 Comments