Solved

Redirect if sub domain folder does not exist

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

12 Experts available now in Live!

Get 1:1 Help Now