Remove sub-domain access from Addon Domain
Most of the shared hosting that allows hosting of multiple sites from a single hosting account (like Hostmonster, CirtexHosting etc.) has a common structure. You'll have one main site/domain and all the other sites/domains will be added as Add-on domains from cpanel. This way you can host multiple sites 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.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.
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.
Pings From:
» how to create a subdomain using cpanel tutorial | Host Rage
» Do I need to purchase new webhosting or can I use an existing one I have to host multiple websites? | Host Rage
» What’s a good free file-hosting website that will host an HTML file for me? | Host Rage
» free domain?
» Dealing With cPanel: Setting Up a 301 Redirect to Your Main Domain And Preventing .htaccess Hell | The One with the Thoughts of Frans
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!