Forum Moderators: phranque

Message Too Old, No Replies

Direct Parked Domain To Specific SubDirectory

Direct Parked Domain To Specific SubDirectory using .htaccess

         

Springwolf

11:36 am on Apr 8, 2010 (gmt 0)

10+ Year Member



I need a little help with directing a subdomain to a specific subdirectory on the parent domain. I'm being limited because my webhost uses cpanel to manage subdomains and redirects. So I'm hoping I can do what I want in the .htaccess file.

Here's what I want to do..
1. I have abc.com as the parent domain.
2. I have parked xyz.net as a subdomain of abc.com
3. I want to preserve xyz.net in the url for my visitors when they visit the site. So I don't want to redirect xyz.net to abc.com/xyz.

The easiest thing would be to set the docroot for the subdomain to the xyz subfolder; but cpanel won't let me do that and I'm not able to update apache for the host provider.

So is there a way to do this in .htaccess. In doing some research I think I found a solution; but it's not exactly what I want..at least I don't think it is.

I found:
RewriteCond ${HTTP_HOST} !^www\.xyz\.com
RewriteCond ${HTTP_HOST} ^([^.]+)\.xyz\.com
RewriteRule (.*) /xyz/ [L]

Is this close?

jdMorgan

12:26 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you set up these subdomains as "add-on domains" in cpanel, then they should already be mapped from URL <subdomain>.example.com/file.abc to server filepath /<subdomain>/file.abc

That's how cpanel 'defines' add-on domains, and you cannot change it (unless you change your hosting and get rid of control panels entirely -- see below.)

If you wish to map subdomains to subdirectories in any other way, then you'll likely need an IP-based virtual host (a.k.a. "private IP address shared hosting") so that all (sub)domains resolve to the server, and it is then left to you to map them into the filesystem below documentroot as you see fit. In that set-up, a common .htaccess code solution (which resembles yours) is:

RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST) ^([^.]+)\.example\.com
RewriteRule ^(.*)$ /subs/%1/$1 [L]

Here, the "/subs" directory is an arbitrarily-named 'master' subdirectory that contains all of the subdomains' subdirectories, and its primary function is illustrated by the first RewriteCond -- Its presence in the Req_Rec path prevents an infinite rewriting loop without requiring that each subdomain-subdirectory be pre-defined, listed, and checked by the code.

Jim

Springwolf

1:11 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Thanks for the quick response Jim.

I didn't want to use 'add-ons' as I don't want xyz.abc.com as my url. So it's either redirect or another hosting package which I'm trying to avoid.

I don't have additional parked domains. Just this one. I guess I'm going to be stuck with using cpanel to do a redirect....which sucks.

jdMorgan

1:50 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using an "add-on domain" does not imply or require any disruption of the URL used and displayed in the client browser. This is implemented using an internal rewrite (specifying a filepath) and not an external client redirect (specifying a URL).

Investigate whether my assumption about your cpanel "add-on" set-up is correct. If it is, then you've already got what you need to put the subdomain-to-subdirectory mapping in place. If not, then you may already have an IP-based server, and that bears investigation as well.

Given "typical" hosting account offerings, you can either use the "add-on domain" function of cpanel to transparently map the subdomain URLs to a subdirectory in the filesystem, or you will need to pay the typically-$1.00-a-month-or-so fee to upgrade to a unique-IP-address hosting account. Leaving out the more-expensive VPS and dedicated-server options, these are your two choices with typical hosting companies using cpanel.

Please note the important distinctions here between external redirects and internal rewrites, and between URLs versus files/directories in the server's filesystem. These distinctions are critical to understanding the problem and correctly implementing a solution.

Jim