Solved

Redirect if sub domain folder does not exist

Posted on 2006-10-19
3
1,345 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
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 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now