views:

302

answers:

2

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:

/home/user/public_html automatically redirects to http://www.example.com

Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'

/home/user/subdomain needs to redirect to http://subdomain.example.com

How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess

A: 

Try this rule:

RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]

But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Gumbo
+2  A: 

I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]

I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.

Good luck!

One Crayon