InMotion Hosting Support Center

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.

Support Center Login

Social Media Login

   
Social Login Joomla

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
16,968 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-20 4:31 pm

Great job, There are another tutorial about .htaccess, it provides some useful examples, http://www.bewebdeveloper.com/tutorial-about-advanced-htaccess-examples

Staff
16,968 Points
2014-11-28 12:58 pm
Hello

Thank you for your suggestion. Feel free to submit a post in our User Submitted section. I can then link to your post.

You can then list yourself as a Contributor to the Support Center.

Thank you,
John-Paul
n/a Points
2014-10-22 4:55 am

Thank You very much for your 3lines codes, it helps me a lot.

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
28,219 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
n/a Points
2014-10-22 8:00 am
Great :)
2014-11-28 2:35 am
Hello IMH. I'm a IMH subscriber.

I can no longer access a subdomain, say, blog.example.com after forcing example.com to www.example.com in .htaccess file. Please how do I resolve this? I prefer to display the subdomain as blog.example.com instead of www.example.com/blog. Thank you.
Staff
16,968 Points
2014-11-28 1:00 pm
Hello Ndiana,

Thank you for contacting us. There may be an existing rule interfering with the ones you are adding from this article. I recommend reviewing the existing rules for incompatibility issues.

Also, it may be easier to make this change in your CMS, for example are you using WordPress, Joomla, or Drupal?

Renaming your .htaccess file (such as .htaccess.old) should allow you to regain access to your site.

Thank you,
John-Paul
n/a Points
2015-01-14 1:21 am

my website is not opening in mobile devices it redirect in mob-update.ru/enupdate. please help me what can i do.

 

Staff
28,219 Points
2015-01-15 11:29 am
Hello Rahul,

This article is only about forcing www or non-www redirects, not specific redirect issues. I have moved your comment to be a new question here. You will want to start a new question in the future.

Kindest Regards,
Scott M
n/a Points
2015-01-30 7:30 am
You are the best. I resolve my problem. Thank you very much.
n/a Points
2015-02-24 8:30 am

Hi,

 

How to redirect: https://www.example.com to https://example.com

I can do this for http but not for https.

 

Thank you,

 

Hassan.

Staff
16,968 Points
2015-02-24 9:41 am
Hello hassan,

Thank you for your question. Most likely an existing .htaccess rule is interfering with the one you are adding.

If you are using a CMS (such as WordPress, Joomla, Drupal, or Concrete5); you may have to change the URL in the config file, or use a plugin/extension.

Thank you,
John-Paul
n/a Points
2015-03-14 5:25 pm
My website redirects non-www http:// to https:// but with a www it simply stays at http:// and I just cannot find a way to do both.
Staff
16,968 Points
2015-03-26 9:29 am
Hllo Justo,

Thank you for contacting us. If the redirect rules are not working, most likely an existing rule is interfering, or you have to make the change in your CMS.

Are you using a CMS, such as WordPress, Drupal, or Joomla? If so, you will have to update the site URL in the settings/Admin dashboard.

Thank you,
John-Paul
n/a Points
2015-03-25 11:10 am
We'll all have to make the transition to https before long. I'll need a guide for that as there will be four domains then : http://www.site.com ; http://site.com ; https://www.site.com ; https://site.com
n/a Points
2015-03-26 3:51 am

Hi Jon,

Your input is fine for forcing non-www to www.

How about redirecting all http://example, https://example to  https://www.example.com

Staff
16,968 Points
2015-03-26 9:43 am
Hello Jason,

Thank you for your question. Please see our guide Forcing your Website's visitors to use the shared SSL. Instead of redirecting to the shared SSL, force your SSL site, such as https://www.example.com.

If you are using a CMS, you will most likely have to update the site URL in the settings/Admin dashboard.

Thank you,
John-Paul
n/a Points
2015-03-26 5:15 pm

Hi Jon, 

I am using Joomla and I moved all my web files from another host as your hosting was recommended still I can't make .htaccess work properly with Joomla I want friendly urls now I had to disable it so website works coz when I enable friendly urls all links are broken and it shows 404 msg can you please help? 

Staff
28,219 Points
2015-03-27 10:45 am
Hello rei,

Have you checked the htaccess to see if you need to remove any residual redirects before resetting the SEO friendly URLs?

Kindest Regards,
Scott M
n/a Points
2015-04-16 1:08 pm

Thank you for the reply my comment wasn't showing in this forum no idea why! well it worked once I forwarded the domain! :) thank you for the reply 

n/a Points
2015-03-29 2:59 pm

Thank you so much... its saved a lot of time..

n/a Points
2015-04-08 4:43 am

Thank you for the sharing Helpful information. its save too much 

n/a Points
2015-04-16 11:58 am

Thank you verry much. You help me a lot. I published in my own blog and give the credits for you. Thank you.

n/a Points
2015-04-18 10:45 am

This worked well with my LINUX server, however it did not work with my clients WINDOWS server

WHAT WORKED

#Force www:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^affordablewebsites.ws [NC]

RewriteRule ^(.*)$ http://www.affordablewebsites.ws/$1 [L,R=301,NC]

 

WHAT DID NOT WORK - WINDOWS

 

#Force www:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^charliessports.com [NC]

RewriteRule ^(.*)$ http://www.charliessports.com/$1 [L,R=301,NC]

 

Thanks anyway for the info and any help would be appreciated

Staff
28,219 Points
2015-04-20 8:35 am
Hello,

The article here does assume a Linux environment as it is for htaccess. We do not have any Windows servers to test on so we cannot confirm the exact code for a Windows/IIS setup.

Kindest Regards,
Scott M
n/a Points
2015-04-20 10:43 am

Thanks a million Scott for your reply, I only have that one client that is on a windows platform and I advised him on Saturday that a solution may not be near.

n/a Points
2015-05-22 8:12 am

thank you http://www.inmotionhosting.com.

n/a Points
2015-06-17 2:25 pm

Hi Inmotionhosting.

I just used the above method to redirect our website from evermart.dk to www.evermart.dk and it worked great - thank you!

n/a Points
2015-06-29 2:08 am

Hi,

Anyone help me,

to redirect from 'sitename.com' to 'http://www.sitename.com' my site is in wordpress platform.

Please help me its urgent.

Thanks,

Staff
28,219 Points
2015-06-29 1:38 pm
Hello poojali,

You can change your WordPress URL by following this article.

Kindest Regards,
Scott M
n/a Points
2015-07-01 2:17 am

Hello Scott,

Thanks for the suggestion. 

Here in my site i have set the site url in this format 'https://www.sitename.com' format, but generally if any one come to visit my site then he/she not enter the url that starts with http or https, they simply enter 'sitename.com' so i want to redirect that to 'https://www.sitename.com'  because my site url is 'https://www.sitename.com'.

So please provide solution if available.

 

Thanks,

Poojali

Staff
25,386 Points
2015-07-01 9:22 am
Hello Poojali,

First you'd have to make sure that HTTPS is properly setup for your WordPress site. After that you would need to force https through the .htaccess file. You can also use a WordPress plugin such as this one to force SSL.

I hope this helps to answer your question, please let us know if you require any further assistance.

Regards,
Arnel C.
n/a Points
2015-07-05 4:07 pm

Thank you so much.

n/a Points
2015-07-13 6:21 am

thank! thank you very much!!!

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.

38 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!