Link to home
Start Free TrialLog in
Avatar of pD_EO
pD_EO

asked on

Redirect if sub domain folder does not exist

Hey guys,

Have been trying to work this out for myself using mod_rewrite in a .htaccess file, but just can't seem to do it.

Basically if someone was to goto the URL...

http://subdomain.domain.com

...and "subdomain" does not exist, then send them to the "domain.com".  Problem is that "subdomain" is unknown, as they are created on the fly, so I need a way to test that the folder exists, if it doesn't redirect, though if it does, display that website for that subdomain.

Hope I was clear.

Thanks :)
Avatar of www-i-p-mu
www-i-p-mu

Hi pD_EO,

what do you mean by "Problem is that "subdomain" is unknown, as they are created on the fly, so I need a way to test that the folder exists".

if you want to make any subdomain that is created to go to your domain then you need to create a wild card DNS Broadcast for your domain name for example:

if you have the domain name http://domain.com pointing to an IP and you want that if people write http://test.domain.com it redirects to your domain then create the following wildcard:

*.domain pointing to the same IP as domain.com - note the asterix means that everything in front of .domain will redirects to domain.com

Hope this helps,

Regards
Avatar of pD_EO

ASKER

Hey,

Will try to explain it again.

You know at remotely hosted places, like forums for example, the give you a domain like...

"theforum" is just an example name.

http://theforum.domain.com

So the subdomain "theforum" is created when the user signs up, and this could be anything, and there could be 100's of subdomains.  Sometimes these subdomains get deleted for violating certain agreements, so when you try to access http://theforum.domain.com, it will redirect you to http://domain.com because the subdomain does not exist.

I am on shared hosting, so I can only create a .htaccess file, and was hoping to use mod_rewrite to test that the subdomain folder exists, if it doesn't, then redirect the user to the main domain.

Here is what I have so far, but it doesn't work.

Options +FollowSymLinks

RewriteEngine On
rewriteCond %{HTTP_HOST} ([^\.]+)\.MY_DOMAIN\.com [NC]
rewriteCond /home/USER_HERE/public_html/%1 -d
rewriteRule ^(.*)$ http://MY_DOMAIN.com [L,R]

I only know basic mod_rewrite, this is a little past me at the moment.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial