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:
- 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.
- 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).
- 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.
- 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 OnRewriteCond %{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 OnRewriteBase /
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 OnRewriteCond %{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.
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:
- Removes the subdomain access from your addon domain.
- 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 OnRewriteCond %{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 OnRewriteCond %{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.]
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.
HOSTGATOR:
I thoroughly tested the solutions you mentioned in updates one and two and found that they are still not the ideal solution and the reason why is not obvious. I'll do my best to explain.
The htaccess you suggest basically says that ANY site requesting ANY file from addondomain.com will trigger a 301 redirect TO addondomain.com. example: addondomain.maindomain.com will redirect to addondomain.com. If a competitor goes and links to addondomain.maindomain.com/(insert adult words here) your htaccess examples will tell search engines that it is ALL 301 to your site. You'll end up with hundreds and perhaps thousands of urls 301'd to your addon domain, and that's not good.
A BETTER solution. In the htaccess file of maindomain.com add code that returns a 404 error for any attempts to reach ANY subdomain. trying to reach addondomain.maindomain.com would result in a 404 error and fail. You'll also want code that does the same in each of the addondomain htaccess files, if the host is not addondomain.com then 404. That way when maindomain.com/addondomain.com/index.php attempts to load it will not be found so visiting maindomain.com/addondomain.com will trigger a 404 error.
PROBLEM - this may or may not break the ability for people to reach the addon domain via addondomain.com, if it does then you'll need to modify the DNS files and create either an a record or a cname record.
My OPINION: This is all really stupid, a host should know better. If someone wants 2 sites on one hosting plan they need to avoid making the 2nd domain reachable by a subdomain or folder of the 1st domain... Godaddy gets that right. Hostgator gets header responses right however, no silly expires in 1981 must revalidate stuff you cannot remove like on Godaddy. (they also do not let you unset pragma... just wow).
Yours is, however, the best article on the subject right now, thanks!
I might be completely in the wrong but I found a completely different approach that only takes seconds and needs no coding or .htaccess.
I just removed all the ns-records for the unwanted (automatically generated) subdomain, in the advanced zone-editor under my main domain.
example:
maindomain.com 3600 IN A 10.10.10.10
sub.maindomain.com 3600 etc.. (remove all these ns-records)
This seems to have solved this issue completely. Since no dns-servers will find any ns-records regarding the subdomain, there will be no erroneous content, no negative points on the domain and no one will mistakenly "land" on that domain adress since it will not be found on any searches. Hence no need for redirections or anything..
The subdomain will just be a record in CPanel and have no meaning whatsoever.
If I'm completely wrong, please tell me. Otherwise this seems to me, to be the quickest and best solution.
If it works then fine, I don't see anything wrong in it. However, not all the hosts let you edit NS records like this. Also, there might be other DNS record pointing to the subdomain. So makes sure your addon domain's DNS is not being affected by it anyway.
To be on the safe side, ask your hosting provider's support if this will create any problem. Clear browser DNS cache and check a couple of days later. If it still works, then you are file.
Thanks Fayaz!
Ok. I'll wait and see. At least so far, it seems like everything's fine.
Oh, and great tutorial btw, I set it up like you explained at first and it worked great but then I had 16 more domains I had to do it with and I started getting annoyed.. So I deleted all records out of spite and noticed everything seemed to be working still..
Hey, this is nice info, but it is not working on all links.
For example if i have main.com and addon.com, then someone can enter this:
addon.main.com/something and there won't be any redirect.
Any solution for that?
Assuming you've followed the instructions correctly, for addon.main.com/something it'll redirect to http://www.addon.com
Please read the post carefully again. If you still can't figure out, copy the htaccess file content from addon.main.com here.
Very helpful article. I want to ask, says I have 3 addon domain (three other domains), how to write the code? Thanks in advance.
For Domain-1:
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.domain-1.com
RewriteRule ^(.*)$ "http://www.domain-1.com/" [R=301,L]
For Domain-2:
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.domain-2.com
RewriteRule ^(.*)$ "http://www.domain-2.com/" [R=301,L]
For Domain-3:
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.domain-3.com
RewriteRule ^(.*)$ "http://www.domain-3.com/" [R=301,L]
Thank you very much. this very helpful 🙂
Just found out I have this exact problem as was very lucky to find your article on this. I have a regular site and my add on domains all have this problem. I found this out today so will be trying to add code to my .htaccess file. Thanks
You are a life saver! I was about to give up on cPanel entirely because of this issue. Thanks a million.
I did it differently. From start, I created a bogus maindomain, which I gave a strictly improbable name to ever exist on the Internet. An example of such a impossible domain name would be "bogusdom.bullsh". So, that's the cpanel envelope (hierarc) for all further addon domains. As a result, nobody can access my websites from address "bogusdom.bullsh. anotherdomain.ext". And, even if "bogusdom.bullshit" starts existing one day, it will not offer my domain as a subdomain. This organization doesn't impair any website access. But "bogudom.addondomains.ext" are being added automatically for each new addon domain and subdomain. And, that's a lot of useless subdomains in the subdomains and domains lists to look at, which makes the whole difficult to manage. And, it must generate a lot of useless code lines for Apache...
My actual reason to come to this very interesting discussion is to determine if I can safely remove (erase) all those bogus cpanel created subdomains such as "myaddondom.bogusdom.bullshit", which I have no use for. I guess, I'll try a couple and see what happens.
Wow! that was what I was exactly looking for. I really wanted to remove subdomain for main domain.
How to Access subdomain concept throught maindomain without any includes files
I need to access the subdomain by htaccess
This is very useful I think i am gonna add this to my site at some point.
This is a very valuable resource of information! I have tried adding this line of code in the .htaccess in the root domain but since I have multiple addon domain in a single hosting, the best way for me to do it is to put the code in the htaccess file in every add on domain. It works perfectly! Thank you for this post.
Thanks for this information. Sadly many other google page one search results for this same topic offer little detailed information. I'm so glad I found your site, and appreciate your tutorial very much. I'm sick of my hosting company and am desperate to stop the rot (my site dropping down in google search) which I believe is being caused by my host's constant 500 server errors. I was on Page 1 a week ago, and have a PR1 ranking, but my host can't keep my site up long enough for a user to view a few pages. It is SO frustrating. I've never had any problems with Hostgator who host a number of my other addon domains, but I didn't have the first clue how to go about moving my addon domain over to my other host. Appreciate the effort you go to, to inform others! Thanks
In the solution you have suggested
RewriteCond %{HTTP_HOST} !www.main-domain.com
Where as in update you have suggested the same line as
RewriteCond %{HTTP_HOST} !www.another-domain.com
Is this correct? Or the line in update should also rewrite the condition for main-domain?