PDA

View Full Version : Redirecting "pointed" domains based on domain name



CLBridges
11-10-2003, 05:40 PM
Can the htaccess file redirect "pointed" domains to a specific URL based on the domain name requested? (Gosh, that doesn't make any sense to me either!)

For instance.. my main site is www.maindomain.com and I have www.subdomain1.com and www.subdomain2.com both pointed to the same IP address as www.maindomain.com. Without being redirected, requests for all 3 domains reach the same index.htm page in my root directory.

Using htaccess, is it possible to redirect
requests for www.maindomain.com to /index.htm, and
requests for www.subdomain1.com to /subdir1/index.htm, and
requests for www.subdomain2.com to /subdir2/index.htm,

Or can only the relative path be used for the URL being directed?

I'm sorry.. I wish I knew the technical terms for all this.. :(

All help is appreciated.

Thanx! Carrie**

AlexBel
11-11-2003, 04:40 PM
Hi Carrie,
If you use Apache, you can write statement DocumentRoot for each VirtualHost in httpd.conf
For expample:


NameVirtualHost www.subdomain1.com
<VirtualHost www.subdomain1.com>
DocumentRoot "your_path/subdir1"
ServerName www.subdomain1.com
ErrorLog logs/web-error.log
CustomLog logs/web-access.log common
ScriptAlias /cgi-bin/ "your_path/cgi-bin/"
</VirtualHost>

CLBridges
11-12-2003, 11:43 AM
If you use Apache, you can write statement DocumentRoot for each VirtualHost in httpd.conf

Hi Alex.. I was afraid of that :( I don't have access to the httpd.conf file. I knew it could be done somehow because a site I used to maintain a long time ago was set up that way. I've already asked our host if it could be done, but they told me no. Probably just trying to get more money outta me!

I'll pass the code you provided along to their tech support department with a copy of my original request and their reply asking "Why NOT?"

Thank You for the info!

Carrie**

Egg
11-13-2003, 06:02 PM
I've tried the same idea, and never got it exactly with .htaccess ( so I handled it with PHP )
The biggest problem I found, was that it Visibly redirected ( ie: the addy in the address bar changed ) instead of doing it 'silently / invisibly'

Anyways, I think you may have to use MODREWRITE in the long run, here is the Apache page for it:
http://httpd.apache.org/docs/misc/rewriteguide.html

The basic idea will be
1) it checks the domain name ( HTTP_HOST )
2) it redirects to the subdir.

I hope this helps start you off, but this topic could really use more expansion on it by someone with some modrewrite experience!

Good luck!

AlexBel
11-13-2003, 06:31 PM
Anyways, I think you may have to use MODREWRITE in the long run, here is the Apache page for it:
http://httpd.apache.org/docs/misc/rewriteguide.html

The basic idea will be
1) it checks the domain name ( HTTP_HOST )
2) it redirects to the subdir.

Yes, Why not?

williamjay
11-22-2003, 10:55 AM
How about??

RedirectPermanent www.subdomain1.com /subdir1/
RedirectPermanent www.subdomain2.com /subdir2/

William

jdMorgan
11-23-2003, 12:03 AM
Carrie,

The following code may be useful, although your requirements description had some ambiguities in it. I'm not sure you really want to redirect *all* requests for *any file* in a particular subdomain to the index.html file in the corresponding subdirectory, so here's what this code actually does; you can tweak it as needed:

If www.maindomain.com/<whatever> or maindomain.com<whatever> do nothing - serve the requested file.

If www.subdomain1.com/<whatever.file> or subdomain1.com/<whatever.file>, redirect silently to /subdomain1/<whatever.file>

If www.subdomain2.com/<whatever.file> or subdomain2.com/<whatever.file>, redirect silently to /subdomain2/<whatever.file>


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]*)\.com
RewriteRule ^(.*)$ /%2/$1 [L]

The Options line may not be needed if your server configuration already enables FollowSymLinks -- try it with and without.

Similarly, RewriteEngine on is only needed once in your .htaccess file -- before any rewrite directives.

Jim

Egg
11-24-2003, 07:48 PM
The 'silent redirection' aspect can be very important to the feeling of your visitors 'safety, and being in control' thank you for that elusive modrewrite code!

As for you Carrie, if you wanted the simple, non-invisible PHP code redirection, you make a .php file that goes like this:

<?php
// First step, decide using the domain name which
// directory you want to redirect to

$redirtothisfile="default_index.html";

if ($HTTP_SERVER_VARS['HTTP_HOST']==="dogs.com")
{$redirtothisfile="/dogs/dogindex.html";}

if ($HTTP_SERVER_VARS['HTTP_HOST'] ==="purbreddogs.com")
{$redirtothisfile="/dogs/purbreadindex.html";}

if ($HTTP_SERVER_VARS['HTTP_HOST'] ==="cats.com")
{$redirtothisfile="/cats/index.html";}

if (SOME_OTHER_FACTOR) {$redirtothisfile="SOMEFILE"}

// 2) now add the 'HTML HEADER' redirection
// see the PHP manual for header() here:
// http://www.php.net/manual/en/function.header.php

header("Location: $redirtothisfile");
die;
?>

And that's it! The browser will redirect with this, the script will end, and you'll see the new page etc. . .

CLBridges
11-26-2003, 08:24 AM
Yes, invisibility is an important part of what I am hoping to achieve. Everyone has been very helpful.. thank you! I'm still waiting.. er, "corresponding" with them about it.. (read: asking WHY NOT??)