Remove sub-domain access from Addon Domain
Most of the shared web-hosts that allow hosting of multiple sites from a single hosting account (like Hostmonster, CirtexHosting etc.) has a common structure. You'll have one main website or domain and all the other websites or domains will be added as Add-on domains from cpanel. This way you can host multiple websites from a single hosting account. However, this approach has a problem. With each Add-on domain created from cpanel, you also automatically get a sub-domain (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 Add-on domain option in cpanel, by creating a sub-domain of the main site and then pointing that sub-domain to the newly created Add-on domain. So in a way, the Add-on 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. Now you want to host another site, named www.another-domain.com, from the same hosting account. This can only be done using the Add-on domain option. When you add this new domain with the Add-on 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 sub-domain (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 Add-on 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 sub-domain (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 sub-domain and the main domain, But the sub-domain 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 sub-domain 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 Add-on 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 Add-on 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 Add-on domain will essentially be visible from its original domain only. If anyone tries to access it with the sub-domain, then he will be redirected to the main domain. So the browser will always show your main site's address, instead of the sub-domain. Besides, with the new code in place, search engines will never tag your site with the sub-domain. So most likely, no one will even try to access your site with the sub-domain.
So, with this new .htaccess code, fell free to add as many Add-on domains as you like, with just one hosting account.
If, Google is already showing your unwanted sub-domains, 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 sub-domain 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:
- 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.]
Update-2:
Another blog post suggested a slightly different solution, which will allow to soft-link your addon-domain with the main site's sub-domain. 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 sub domain; and in essence, deny anyone to use your site from the sub-domain. 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.
Leave a Reply
Mar 30th 2010 • 09:03
by nurul amin russel
cool - very useful tip
thanks for sharing
Apr 1st 2010 • 14:04
by Lochnload
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 !
Apr 10th 2010 • 17:04
by forex robot
What a great resource!
May 5th 2010 • 10:05
by Joe Mason
It is also easy to backup and transfer all your websites from one server to another server if you have cPanel installed~~,
May 30th 2010 • 00:05
by Biro Jasa
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
Jun 8th 2010 • 15:06
by Ruben
This is exactly what I was looking for, thank you very much. Greetings from Peru.
Jul 29th 2010 • 17:07
by Frans
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]
Aug 1st 2010 • 14:08
by fayazmiraz
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)
Aug 1st 2010 • 12:08
by Matt
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?
Aug 1st 2010 • 14:08
by fayazmiraz
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.
Aug 18th 2010 • 23:08
by Tom
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!
Nov 5th 2010 • 04:11
by Leonardo
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!!!
Dec 27th 2010 • 21:12
by Casino Hire UK
Thank you, I stumbled across this and i have been looking for a dummy proof solution for ages as My subdomain address shows up on google searches all the time and it looks terrible
Jan 18th 2011 • 21:01
by sinner
Thank for the post you save me time and headache.
Mar 2nd 2011 • 11:03
by nvprogman
Thanks for the post! This did exactly what we needed to do.
Apr 4th 2011 • 07:04
by Sweetcorn Pancakes
I have been scouting around for this particular insight, thank you! What other sites can you think I should check out? Websites like this are difficult to come across, make sure to continue the great work.
Jun 28th 2011 • 21:06
by Peter
Thanks Fayaz, i have only just started using add on domains rather than hosting each site individually and been looking for a solution for this as been worried about a potential google duplicate content penalty. Not using the hosting companies you mentioned but fingers crossed will work through cpanel with my host.
Jul 1st 2011 • 02:07
by Casino Hire York
I use host papa, and I cannot get it to work the way you describe, although I am sure it is my low tech knowledge
Aug 1st 2011 • 10:08
by Noida
Very Very helpful , thank you sir , it was very helpful , and i used it in my add-on domains . Actually i don't understand .htaccess much . but thanks you made it easy for me . bye ,, take care...
Oct 3rd 2011 • 00:10
by Richy
Hi I have a .co.za domain and this solution keeps giving me an error of
page not redirecting correctly, is there something im doing wrong?
Oct 3rd 2011 • 13:10
by Fayaz Ahmed
Hi Richy,
I think .co.za has nothing to do with the error. Possibly you've implemented the CODE wrongly!
Say your site name is http://www.domain.co.za, in that case place the following CODE on this site's root folder. Remember NOT on the main site's root, rather the addon-domain's root. Say your main site is YourSite.com and http://www.domain.co.za was added as an addon domain with domain.YourSite.com subdomain. In that case, the subdomain's folder is the root folder for your co.za addon domain.
Now that you know where to put the CODE, put the following CODE in your .htaccess file (if it is not a wordpress installation):
RewriteEngine On
RewriteCond %{HTTP_HOST} !www\.domain\.co\.za
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.co\.za\/" [R=301,L]
If it's a wordpress installation or if your site contains preexisting htaccess CODE, then remember to adjust it accordingly.
If my suggestion still doesn't work, then I'll need to see how your site is implemented.
Oct 3rd 2011 • 12:10
by Ronald
How about you just delete the Zone Record, that way that subdomain won't exist at all
Oct 3rd 2011 • 13:10
by Fayaz Ahmed
That might work, however I've seen some site to implement the zone record such a way that without the subdomain, addon domain would not work.
So if you just delete the zone record of the subdomain, most likely the addon domain will stop workin in those cases. The thing is, they don't add the subdomain as a convenience, rather it's a way for them to implement the addon domain.
Also, not all the hosting company will let you edit the zone record. So I guess you're stuck with this kinds of implementation in most cases.
Oct 8th 2011 • 20:10
by Jonas Brothers
Don't you think returning a 404 is much better than 301? If you return a 301 and the user gets redirected to the proper subdomain URL, atleast he would know that he got your mainsite URL correctly (because it was redirected, instead of getting an error). He can now concentrate attacking your mainsite domain since he knows all your subdomains are under it.
Oct 21st 2011 • 20:10
by rob
Great article, very nicely done, you should also include using htaccess to rewrite a subdomain, as most people will want that. just a thought.
cheers.
Oct 21st 2011 • 20:10
by rob web design cardiff
another afterthought, does anyone know how to implement the add on domain option in cpanel, using htaccess for a hosting account that does not support cpanel? Tried framed redirects, but this still showed the original hosting domain.
any help would be great on this one.
Oct 27th 2011 • 18:10
by Monalisa Smith
Nice information.....thanks.
http://www.hosts101.com/
Nov 9th 2011 • 18:11
by John Kayde
Nice post! Thanks! The solution works, but the subdomain still exists, and some search engines may index it. Here's the solution how to completely remove the subdomain: http://upcode.wordpress.com/2011/11/09/cpanel-addon-domain-without-subdomain-on-your-main-account/
Nov 11th 2011 • 09:11
by fritzi
Thank you so much, I've been searching for this info at hostgator, but every has thier own solutions, including robot files. This solved my issue. Thanks for the informative post.