CPanel Addon Domain Redirect

So you just bought a new domain name but you don’t want to buy another webhosting plan because your current webhosting allowed you to host multiple domain under one account so you decided not to buy another hosting. Although addon domain can be set up easily, but depending on your host configuration especially if they use CPanel and didn’t change where the addondomain location should be placed (in this case the addon domain was placed as a sub-directory in your primary domain when you registered with them), you might know that you can access your addon domain name like below for an example:

www.mydomain.com/addondomain
mydomain.com/addondomain
addondomain.mydomain.com
www.addondomain.mydomain.com
www.addondomain.com
addondomain.com

And if that’s bothering you, the you can use this .htaccess code. Just make sure to place the .htaccess inside the root of your addon domain directory for example /public_html/addondomainname/.htaccess. And feel free to adjust whether you want www placed in front of your addon domain name or not (assuming that you have configured www in your DNS configuration)

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

Don’t forget to change the addondomain.com into your real domain name and make sure that you have mod_rewrite enabled (if you get server error 500 that means mod_rewrite is disabled by your host, ask them to enable it)

Note: this post was updated on 21 Oct, 2011

Comment?

Note: Comment may not appear right away.

21 comments on “CPanel Addon Domain Redirect

  1. Hello! Do you know if they make any plugins to help with SEO?
    I’m trying tto get mmy bblog too rank for
    some targeted keywords but I’m not seeing very
    good gains. If you know of any please share. Many thanks!

  2. Check your mail.

    While you updated this post, you posted the lines I told you to “drop” (meaning: delete). What you posted now shows the lines to delete but ignores the lines you should have kept.

    Anyway, a working example can be found in your inbox – with ample comments. Enjoy the correction! ;)

    Best,

    Mike

    1. Ah yes actually i understand your first comment about dropping the unneeded two lines code and i understand the code :)

      Also I tried to simulate how the CPanel addon domain work on my local apache server (because i don’t have access to the cpanel on my local machine, obviously lol) for testing with below configuration:

      Primary domain:

      ServerName primary.lan
      ServerAlias http://www.primary.lan
      Document root: /home/user/primary

      Addon Domain:

      ServerName addondomain.lan
      ServerAlias http://www.addondomain.lan addondomain.primary.lan http://www.addondomain.primary.lan
      Document root: /home/user/primary/addondomain

      The http://www.addondomain.primary.lan is configured on serveralias because i remember back then that you can access your addon domain name as if it’s a subdomain of your primary and then AllowOverride is set All

      and the .htaccess file / rule is placed / added to:
      /home/user/primary/addondomain/.htaccess

      The old code:

      RewriteEngine On
      
      RewriteCond %{HTTP_HOST} ^(www\.)?addondomain\.primary\.lan [NC,OR]
      RewriteCond %{HTTP_HOST} ^!www\.addondomain\.lan [NC]
      RewriteRule ^(.*)$ http://www.addondomain.lan/$1 [R=301,L]
      
      RewriteCond %{HTTP_HOST} ^addondomain.lan [NC]
      RewriteRule ^(.*)$ http://www.addondomain.lan/$1 [R=301,L]
      
      RewriteCond %{REQUEST_URI} ^/addondomain($|/.*$)
      RewriteRule ^.* http://www.addondomain.lan%1 [R=301,L]
      

      And because there are unneeded parts that need to be changed and they can be combined. We can change the old code to below code

      Combined and Fixed Old Code:

      # if the old code fixed with by removing unneeded parts and combining them into one
      # but still not efficient as the new rule i think
      RewriteEngine On
      
      RewriteCond %{HTTP_HOST} ^(www\.)?addondomain\.primary\.lan [NC,OR]
      RewriteCond %{HTTP_HOST} ^addondomain\.lan [NC,OR]
      RewriteCond %{REQUEST_URI} ^/addondomain [NC]
      RewriteRule ^(.*)$ http://www.addondomain.lan/$1 [R=301,L]
      

      The new code:

      # notice the exclamation mark is placed at the beginning
      RewriteEngine On
      
      RewriteCond %{HTTP_HOST} !^www\.addondomain\.lan [NC]
      RewriteRule ^(.*)$ http://www.addondomain.lan/$1 [R=301,L]
      

      These three rules are tested with various url as described in this post, and all of them give the same result but i’m sure the new code is much more efficient than the old code even if i removed the unneeded parts and also combined them to a single rule

      Btw … Thank you very much, for the good feedback Mike :)

      p.s I send you another email :)

    1. Thank you so much for pointing that out. After all these years you’re the first person that noticed it :)

      Anyway i’ve updated the post entirely (and also replaced the code entirely not just dropping the two lines you mentioned) just in case someone else are having this issue and somehow found their way to this post in the future. And i also write that not all host are using this configuration (back then when i wrote this post in 2006 i was hosted by Hostgator and they placed addon domain as sub directory)

  3. Whenever I use this code for an addon domain with wordpress I can’t login anymore. Any suggestions? I go to the login screen and it doesn’t let me in, nor does it display an error, please let me know what you think. Thanks.

  4. this was an amazing article and saved me a lot of time! thank you very much. what i would like to know now is how do i set it up for multiple domains? i.e. it works for redirecting one domain to a certain subdirectory, but how do i go about adding another one to another subdirectory? hope that makes sense!

  5. Hi,
    What about when my subdomain is in a different root than the main domain: eg in my case maindomain.fi subdomain.us – what should I write now?
    Saku

    1. I’m not quite sure about your question, did you mean addon domain? because subdomain is not the same as addon domain :)

      Anyway, if you’re asking about addon domain placed in another Document Root, then there’s no need to write anything for your second domain, because it’s not located in the same directory as your maindomain. The only thing you should care about is forcing www or not using www at all on your addon domain (for SEO purpose)

  6. What can I say??? THANK YOU! You are a HERO!

    I have spent about 6 hours trawling the web for help with this and everyone seems to be writing ONLY about how to make SEO url’s from dynamic content…all I wanted was…EXACTLY what you have given me here…and it works beautifully! Thank you SOOO much! I only just found your site but im going to come here in future for troubleshooting! So, thanks again, and have a virtual pint on me…

  7. Thanks for the article! Extremely helpful! The question is – how “friendly” is this code for search engine ranking?
    Thanks!

  8. This article is awesome. I have copied many different pieces of code today trying to make this happen. Not only does this one work but it covers all the possibilities. Extremely helpful.