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.example.com or simply example.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 in your .htaccess in the cPanel for your primary domain.

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. 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.
    file-manager-show-hidden
  4. 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.
    file-manager-htaccess
  5. 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]
  6. 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.
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-06-12 2:40 pm

Thank you so much, Scott!

 

My website, contentsplanet.com was facing canonicalisation issue, couldn't find how to fix it until a solution from a source i came across. it suggested me to fix .htaccess and upon searching on Google, i found your site. (The site still has a lot of SEO-issues)

Thank you for having such a great explantory note.

 

Regards,

Pawan Kr Jha

n/a Points
2014-09-01 4:45 am
Explain how to enable cpanel to show the .htaccess file, which isn't showing in my one.
Staff
10,345 Points
2014-09-18 9:41 am
Hello Rob,

Thank you for contacting us. Please see our guide Where is my .htaccess file?

It includes how to find your .htaccess file, and how to create one if it is not there.

If you have any further questions, feel free to post them below.

Thank you,
John-Paul
n/a Points
2014-10-22 5:00 am
I am new to htAccessm if you can tell us whats NC, L... stands for what then it will be very helpful to understand.
Staff
20,312 Points
2014-10-22 7:45 am
Hello Dhirender,

The NC means 'no-case', or case insensitive. It means it does not matter if uppercase or lowercase is used.
The L means 'last'. When it runs across this, the process should stop where it is and not apply any more rewrite rules.

Kindest Regards,
Scott M

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

8 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!