?
Solved

Redirect if sub domain folder does not exist

Posted on 2006-10-19
3
Medium Priority
?
1,412 Views
Last Modified: 2009-07-29
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 :)
0
Comment
Question by:pD_EO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:www-i-p-mu
ID: 17765043
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
0
 
LVL 6

Author Comment

by:pD_EO
ID: 17765185
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
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 17874762
> but it doesn't work.

You're checking if the directory exists, but not if it does not exist. You must use the exclamation mark  in order to match the condition, if /home/USER_HERE/public_html/%1 does not exist.

RewriteCond /home/USER_HERE/public_html/%1 !-d
0

Featured Post

ATEN's HDBaseT Presentation at InfoComm 2017

Hear ATEN Product Manager YT Liang review HDBaseT technology, highlighting ATEN’s latest solutions as they relate to real-world applications during her presentation at the HDBaseT booth at InfoComm 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question