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
2015-11-25 9:26 am

You solved your problem .htaccess or php redirect , add script in page header, Write about your services, .htaccess support only linux server. so pleae check your server then use. I also use same code in my website

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
24,144 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
24,144 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
38,359 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
24,144 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
38,359 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
24,144 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
24,144 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
24,144 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
38,359 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
38,359 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
38,359 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
33,681 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!!!

n/a Points
2015-08-07 12:01 am

#Force www:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^coconutschool.org$

RewriteRule ^(.*) http://www.coconutschool.org/$1  [QSA,L,R=301]

 

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

 

# END WordPress

It doesn't work, so please tell me how to fix it.

Staff
24,144 Points
2015-08-07 3:40 pm
Hello Socheat,

Thank you for contacting us. Since WordPress has plugins/addons that modify the .htaccess file, I do not recommend adding rules manually for some changes.

To force www on your WordPress website, I recommend updating your Site URL settings. For example, if it is set as "http://example.com" it should be updated to "http://www.example.com" instead.

Thank you,
John-Paul
n/a Points
2015-08-08 7:57 am

Thank you so much.

n/a Points
2015-08-12 5:10 am

Hi there,I'm not sure if this will rectify my problem. My site opens with https://apetslife.co.za but doesn't if you just type apetslife.co.za OR www.apetslife.co.za - this brings an error page from Magento which is the platform we have built on. Are you able to assist?Thanks so much.

Staff
33,681 Points
2015-08-12 11:20 am
Hello Amber,

You may want to clear your browser cache. I tested both the non-www and www based link and they both work. If you're trying to force it, then you should be able to use the tutorial above to force the URL that appears.

If you have any further questions or comments, please let us know.

Regards,
Arnel C.
n/a Points
2015-08-21 5:03 am

I have a problem. I have a website, and i write a content, after a change in wordpress into www. and after, i fix my content, and now, the content with www. different from one with non www.

I'm very worry because i think my problem is very bad.

Please help me!!! Thank you so much!

Staff
38,359 Points
2015-08-21 10:24 am
Hello Rin,

I am not quite understanding your situation. Are you saying that using www.example.com shows different content than example.com? Do you have them pointed to different locations? Have you tried clearing your caches to ensure any changes have taken effect across the board?

Kindest Regards,
Scott M
n/a Points
2015-08-24 1:48 pm
Thanks for sharing this peace of code for redirecting permanently with .htaccess file
n/a Points
2015-08-25 4:53 am

I know about 301 redirection, www to non www or non www to www but please send me code  how i redirect index.php

RewriteCond %{HTTP_HOST}  ^www.example.com [NC]
RewriteRule ^(.*) http://example.com/$1 [L,R=301]

 

Staff
10,077 Points
2015-08-25 9:05 pm
Hello Aaliyah,

Can you please tell me in more detail what you are looking to do? This is code to redirect from www to non-www. This code has nothing to do with your index.php file.

Best Regards,
TJ Edens
n/a Points
2016-01-20 10:51 pm

Hey, thanks for the reply.  Here's how I had to write it or it wouldn't work.

It's missing the carat on the second line, but it works fine.

 

But thanks for the support man! 

RewriteCond %{HTTP_HOST} ^louisvillewebsitebuilder.com$ [NC]

RewriteRule (.*)$ http://www.louisvillewebsitebuilder.com/$1 [R=301]

n/a Points
2015-09-30 9:23 am

Thanks for this nice and useful post.

n/a Points
2015-10-07 4:55 am
  1. Hi,

    I created my website alriwaya.net with Adobe Muse application and I integrated WordPress blogs inside the pages.

    the iframe code is: <iframe name="Stack" style='width: 100%;' src='http://alriwaya.net/blogs/?cat=2' frameBorder="0" overflow:hidden; scrolling="no" seamless="seamless" id="iframe" onload="javascript:resizeIframe(this);"></iframe>

    and I use this script in my page head to let the blog expand in height as articles increased

    <script language="javascript" type="text/javascript">
      function resizeIframe(obj) {
        obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
      }
    </script>

    The problem is when I browse the pagehttp://www.alriwaya.net/%D8%A3%D8%AE%D8%A8%D8%A7%D8%B1.html iframe is not showing and I got error "Uncaught SecurityError: Blocked a frame with origin "http://www.alriwaya.net" from accessing a frame with origin "http://alriwaya.net". Protocols, domains, and ports must match."

    but without www it will show. like that:http://alriwaya.net/%D8%A3%D8%AE%D8%A8%D8%A7%D8%B1.html

    I hope this can be solve.

    Thank you

  2.  

Staff
33,681 Points
2015-10-07 4:33 pm
Hello Khalil,

Sorry for the problem with the site. Adobe Muse is a 3rd party program that will generate the code for you while you design the site. Did you try to use the code provided above for in .htaccess file? You do not indicate that you have tried it. We can't provide coding assistance for your website as that is beyond our scope of support. You may need to work with a developer in order to change your code to accept both versions of the URL, if the .htacess file provided in the tutorial above doesn't already fix the problem.

Regards,
Arnel C.
n/a Points
2015-10-12 9:26 am

Beautiful idiot here... is there a specific place in the .htaccess file that I need to copy the code to or any place is good?

 

:?

Staff
24,144 Points
2015-10-12 9:44 am
Hello Beven,

Thank you for contacting us. Since the .htaccess file is read from the top-down, it can be beneficial to add the rule to the top of the file.

Depending on the other rules you have, it can often be placed anywhere in the file.

Thank you,
John-Paul
n/a Points
2015-10-13 1:47 am

Thank you for that it helped and more importantly it worked... 

 

You both rock!!

Staff
38,359 Points
2015-10-12 9:45 am
Hello Beven,

That is a good question. The .htaccess file is typically read and implemented from top-down. Since this type of code is high priority, I would place it at the top.

Kindest Regards,
Scott M
n/a Points
2015-11-25 2:18 am

Great work! I have been reading your post since long and very much impressed

n/a Points
2015-12-15 2:25 am

Hi,  Thanks for the info.  The main thing I find confusing is how many different versions of this exact code you can find using an htaccess search.  It seems like every website has slightly different syntax. Can you shed some light on this.

Staff
10,077 Points
2015-12-20 9:03 pm
Hello Andrew,

This can be written a few different ways. What exactly are you trying to do and maybe we can help guide you in the right direction.

Best Regards,
TJ Edens
n/a Points
2015-12-23 2:30 pm

I've successfully redirected my domain to https. But when I manually type the address with www(without http/https), its not redirecting. But redirecting well when trying with http or https(with or without www). Is there any rule to redirect even with www.domain.com ?My domain: https://clippingpathin.com, Redirect issue at: www.clippingpathin.comCurrent htaccess:

# BEGIN WordPress

RewriteEngine On

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

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://clippingpathin.com/$1 [R=301,L]

order deny,allow

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

 

# END WordPress

Staff
33,681 Points
2015-12-24 11:29 am
Hello Saif,

Sorry for the problem with your .htaccess. The reason it's not working is because you didn't account for "www" in the URL. In order to take care of that you have to add another rule in your .htaccess file:

RewriteCond %{HTTP_HOST} ^www\.clippingpathin.com [NC]


Once you take the possibility of the www URL, then even if someone uses "www" in the URL, the URL will be re-written as per your re-write rule.

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-12-24 12:52 pm

Unfortunately that's not working. I put that line on the bottom of "Rewrtite engine on" Anything else do you wanna suggest?

Staff
24,144 Points
2015-12-24 1:04 pm
Hello Saif,

Thank you for contacting us. It seems you are using WordPress, which has Themes/plugins that also rely on .htaccess rules. This can create issues when you try to manually add rules (such as redirects).

I recommend setting up SSL within the WordPress dashboard, which is covered in our full guide How to Enable https (SSL) in WordPress.

Thank you,
John-Paul
n/a Points
2016-01-05 2:43 am

can it run on the localhost, i am try but not success;

Staff
24,144 Points
2016-01-05 11:30 am
Hello chandra,

Thank you for contacting us. As long as your PHP handler supports .htaccess files, it should work.

Here is a helpful link to our guide on troubleshooting .htaccess files.

Thank you,
John-Paul
n/a Points
2016-01-06 5:14 am

Hi,

when i am using http://www.example.com then redirect is working fine and it redirects to http://example.com,

but when i am using https://www.example.com then redirect is not working and it is not redirecting to https://example.com.

please help me out in this regard.

Thanks

Staff
33,681 Points
2016-01-06 10:51 am
Hello Pratik,

Sorry for the problem with the re-direct. We would need to see your .htaccess file entries in order to see why it's not working. If you're a customer, then provide us some information on the account and we can look to see what's happening.

If you have any further questions or comments, please let us know.

Regards,
Arnel C.
n/a Points
2016-01-20 8:04 am

Thanks for sharing this nice information . 

n/a Points
2016-04-22 1:49 am

Thanks for providing this straightforward tutorial.

n/a Points
2016-04-28 12:28 pm

Hi Scott..

i am unable redirect my website from http to https.

my website host on linux server and i made it wordpress.. plz help me. i also applied lots of code htaccess file and httpd file

Staff
24,144 Points
2016-04-28 12:35 pm
Hello Arjun,

Thank you for contacting us. Since you are using WordPress which also relies on .htaccess rules, it is not recommend to manually add .htaccess rules.

Instead, just update your WordPress site to HTTPS (SSL) in the Dashboard.

If you are still experiencing issues, try updating your Site URL and Home settings with another method.

Thank you,
John-Paul
n/a Points
2016-05-19 12:00 pm

Hi john,

I am able to redirect in LAN Network my website through the IP address to SSL IP address like http://192.168.2.2 to https://192.168.2.2  but not from website name like http://example.com to https://example .com aslo i want to know you in WAN Network unable to redirect through the ip and website name.. Please give me suitable suggestion..  Thanks

 

Staff
24,144 Points
2016-05-19 1:40 pm
Hello Arjun,

Thank you for contacting us. Since you are using WordPress, again I recommend using a Plugin for creating the 301 redirects.

The IP addresses you are listing are internal, so they will only work on your home network.

Unfortunately, it is difficult to troubleshoot your home network setup, but typically you would forward a port to a specific machine via your external IP address.

Best Regards,
John-Paul
n/a Points
2016-05-11 3:22 pm

Hello,

I'm having some issues with the redirection of my site and can't figure out what is wrong. All I'm trying to get it to do is redirect all non-www pages to their www versions.

Below is the code in the .htaccess file. For some reason, the only thing that is redirecting is the home page. EX: mydomain.com redirects to www.mydomain.com, but mydomain.com/about stays the same and doesn't redirect at all.

Please help!!!

Here is the contents of the .htaccess file:

 

<Ifmodule mod_rewrite.c>

Options +FollowSymlinks

RewriteEngine On

RewriteBase /

  

### re-direct index.html to root / ###

RewriteRule ^index\.html$ / [R=301,L]

RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

  

### re-direct index.php to root / ###

RewriteRule ^index\.php$ / [R=301,L]

RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]

  

### re-direct default.html to root / ###

RewriteRule ^default\.html$ / [R=301,L]

RewriteRule ^(.*)/default\.html$ /$1/ [R=301,L]

 

### re-direct non-www to www ###

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

  

### re-direct any parked domain to www of main domain ###

RewriteCond %{http_host} !^www.mydomain.com$ [nc]

RewriteRule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc,L]

 

</Ifmodule>

Staff
33,681 Points
2016-05-11 3:44 pm
Hello Dusty,

Sorry for the problem with your .htaccess. It has to do with how you're writing your .htaccess file. You need to re-write the rules so that they don't end on the first condition - since you appear to have multiple conditions. Try removing each rule and then adding them one-by-one to see how they're affecting your URL when you go into the site. Remember that the "L" in the rewrite statements means that if that condition applies, it will end at that point.

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

Regards,
Arnel C.
n/a Points
2016-06-14 5:11 am

hi 

i have copied one of the above to forece www  , but without paying attention i copied and pasted the code  without changing any thing  and here you can see how it redirec , here is the domain that i want to forec .

 

newwp.comlu.com

 

be informed that i have replaced example.com with my domain but it sill as you can see when you calick on th domain .

so what can i do and is there way of "re-start server"  or something like that?

 

thank you in advance 

Staff
4,404 Points
2016-06-14 11:07 am
Forcing this change in your .htaccess does not involve any moving of files. I suggest putting back the files to their original order and writing in the .htaccess rules you will see in step 5.
n/a Points
2016-07-18 6:22 am

Terima kasih ya jon,

 

Thank you fery 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.

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