CPanel Addon Domain without Subdomain

It's a very common problem in shared web hosts, check it out and optimize your website by implementing it!

Most of the shared web hosts that allow hosting of multiple sites from a single hosting account (like Hostmonster, Hostgator etc.) has a common structure. You'll have one main website or domain and all the other websites or domains will be added as Addon domains from cpanel. This way you can host multiple websites from a single hosting account. However, this approach has a problem. With each Addon domain created from cpanel, you also automatically get a subdomain (almost in all cases). Now, the challenge is to using the Addon domain, without the auto created subdomain. This may be important for your website optimization.Since, most of the times, they enable this Addon domain option in cpanel, by creating a subdomain of the main site and then pointing that subdomain to the newly created Addon domain. So in a way, the Addon domain is joined together with the subdomain, and doesn't work without it.

Problem Description: For example, you have registered your account with the main site named www.main-domain.com in cpanel. Now you want to host another site, named www.another-domain.com, from the same hosting account. This can only be done using the Addon domain option if you are using cpanel. When you add this new domain with the Addon domain option, they will automatically create a subdomain of the main site (e.g. another-domain.main-domain.com). Now both the newly added domain (www.another-domain.com) and the subdomain (another-domain.main-domain.com) will actually point to the same location; thus the web site content is accessible from both the addresses.

Now, you may not want this for multiple reasons:

  1. You may not want other people to know that you run both the sites www.main-domain.com and www.another-domain.com and specially that you run www.another-domain.com as an Addon domain of www.main-domain.com.
  2. You don't want search engines to tag your site with two different addresses (www.another-domain.com and another-domain.main-domain.com), which may decrease your site's ranking for duplicate contents, divide your site's traffic and even worse - the subdomain (another-domain.main-domain.com) may get better ranking than the original site (www.another-domain.com).
  3. If you run Blog software like WordPress, your site will still be accessible from both the subdomain and the main domain, But the subdomain will send erroneous contents, which may carry a negative point to your site.
  4. There can be many other unforeseeable reasons, But the main point is: You don't want people to visit one of your site as a subdomain of another site.

Solution:

I use an htaccess solution that works both in Hostmonster and CirtexHosting; And it should work in any other hosting site that allows .htaccess with mod_rewrite capability.

So if you have the same problem, then just put a .htaccess file in the Addon domain directory with the following CODE:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} main-domain.com$
RewriteRule ^(.*)$ "http://www.another-domain.com/" [R=301,L]

</IfModule>

Note that, here, main-domain.com is the main site registered with your hosting account, and www.another-domain.com is the new domain you've added to the same shared hosting, using the Addon Domain option.

Of course, you may already have an existing .htaccess file. For example, a tipical WordPress installation will have a .htaccess file with the following CODE:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

In that case just modify the .htaccess file with the following code:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} main-domain.com$
RewriteRule ^(.*)$ "http://www.another-domain.com/" [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

Now, with this new .htaccess file, your new Addon domain will essentially be visible from its original domain only. If anyone tries to access it with the subdomain, then he will be redirected to the main domain. So the browser will always show your main site's address, instead of the subdomain. Besides, with the new code in place, search engines will never tag your site with the subdomain. So most likely, no one will even try to access your site with the subdomain.

So, with this new .htaccess code, fell free to add as many Addon domains as you like, with just one hosting account.

If, Google is already showing your unwanted subdomains, then following this process will eventually remove them. Although if you want them to be removed quickly, then you'll have to create a webmaster account in Google, verify the subdomain as you own and request to remove it.


Update:

Also you might always want others to access your site as www.another-domain.com, instead of just another-domain.com. Since, to search engines, they are two different sites. So, it's always better to fix your site as either www.another-domain.com or another-domain.com.

So, now it's time to update the previous solution, to the one, that does both:

  1. Removes the subdomain access from your addon domain.
  2. Fixes your site to just one address: either to the one with www or to the one without www

Assuming that you'll use www.another-domain.com as your fixed site address, following will be the most optimized htaccess rewrite code to accomplish both the goals:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} !www.another-domain.com
RewriteRule ^(.*)$ "http://www.another-domain.com/" [R=301,L]

</IfModule>

Of course, just as before, for an existing WordPress installation (assuming you have default WordPress htaccess code), the code will look like:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} !www.another-domain.com
RewriteRule ^(.*)$ "http://www.another-domain.com/" [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Also, I'm not sure, but for some people (with multiple hosting from a single account), this Hostmonster KB article may also be helpful.

[This update is attributed to Mahbub vai, as he pointed out a similar solution in Hostmonster Knowledge base. Although code here is more optimized than that of Hostmonster Knowledge Base, still their code actually helped me thinking of a different approach; And from that different approach, I found this even better solution.]


Update-2:

Another blog post suggested a slightly different solution, which will allow to soft-link your addon domain with the main site's subdomain. That's a different solution, but not necessarily the correct one. Since, the main goal is to remove all sorts of soft-linking from the subdomain; and in essence, deny anyone to use your site from the subdomain. So the bottom line is: it is always better if you remove the ability to soft-link any of your perma-links (e.g. a perma-link to a blog post) from the sub domain and force your user to use your site  from its original site address.

72 thoughts on “CPanel Addon Domain without Subdomain”

  1. Thanks Fayaz for the very well written post. It is the most clear post I found among my search results.
    Could you please also post the Update 3 that you mentioned if I am not asking too much 🙂

    Thanks once again!!!

  2. Great resource. Thanks for writing and sharing it. Started noticing Google was listing more and more of my add-on domains and wanted a way to nip it in the bud. Works perfectly!

  3. I used the first update to include both but I'm still able to get to my subdomain .com with subdomain.maindomain.com - Is the first rewrite condition supposed to be main-domain rather than another-domain?

    1. Hi Matt, the first update means: if you try to access this site through any other site (perhaps through sub-domain), then you will be redirected to this site's address.

      So, if you read the post carefully, then you'll notice that http://www.another-domain.com is actually the current site you are trying to access (i.e. the site which was added using the addon-domain option).

      BTW, this htaccess file should be placed inside the addon-domain's http root (not the original hosting account's http root)

      Also, you'll still be able to access the home page only from the subdomain.maindomain.com, but you'll see that the site address in the browser's address bar is changed to it's original domain (instead of the sub-domain). This way search engines will not index them, so most likely people will not access your site through the sub-domain.

      If you still want to completely remove the access from the sub-domain, then you'll have to send a 404 http code, when people tries to access the site through the sub-domain. I'll add code for this in update-3.

  4. Hey, I'm the guy whose blog post you linked to in your second update. I should perhaps clarify that there's also some domain moving (from frans.lowter.us) involved in my case, which used to be where the blog resided.

    However, it's a 301 (permanent) redirect, not a 307 (temporary) redirect, so I'm not sure if I agree with your criticism. With the code contained in your first update it will only redirect subdomain.domain.com to domain.com while subdomain.domain.com/anything will remain perfectly accessible through the subdomain.

    Unless I'm mistaken about what you're saying, you'd still have to add something like this to deny access to whatever one might add after the domain name:

    RewriteCond %{HTTP_HOST} !^domain.com$
    RewriteRule ^.*$ - [F]

    1. Hi Frans, my update was not a criticism, it's just a way of different solution.

      If you read this complete blog post from the beginning, then you'd notice that my goal was to eliminate any sorts of soft linking of the add-on domain to the main domain, and to discourage search engines from linking my site using the sub-domain.

      You are right about 301 redirect, and your solution is more suitable for people whose site
      was already linked through the sub-domain and they intend to keep those link working. Whereas my intention was to completely stop that.

      Initially I even thought of sending a 404 code when people tries to access my site through the sub-domain, then I became a little bit softer and decided to send a 301, but only to the home-page.

      I'm sure both solution works, just with a different tune.

      -------------------
      About the code, since I'm ignoring whatever is after the domain name (i.e. domain.com/anything), if you come to
      the site from sub-domain it will always take you to the original domain's "Home Page" and "Home Page" only.

      For example, if you type: subdomain.maindomain.com/anything, it will take you to http://www.original-domain.com
      (in my example in update-1: original-domain = another-domain)

  5. Pingback: fransdejonge.com
  6. thank you for your tutorials. btw your first way is work well meanwhile the second only resulting error. Maybe because of me that wasn't sure how to put the code or maybe from other 'xxx' factors.

    However thanks, your tutorial very helpful

  7. Pingback: shulslist.com
  8. Pingback: Host Rage
  9. I love that sometimes when you search for an answer to a question you come across a well writen and informative article like this. Thanks for sharing this article.

    Now, time to start re-coding that .htaccess file !

  10. Pingback: Host Rage
  11. Pingback: Host Rage

Leave a Reply

Your email address will not be published. Required fields are marked *