Solved

Redirect if sub domain folder does not exist

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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