Multiple domain redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
some2
Forum Newbie
Posts: 5
Joined: Wed Nov 29, 2006 9:38 am

Multiple domain redirect

Post by some2 »

Hi,

I've a domain working on Apache/2.0.59 (Win32) mod_jk/1.2.18 PHP/4.4.4 setup. It works fine, but I recently configured another domain on the same server. Nameservers and everything is fine but the new domain points to the location of old domain.

Is there any way, using PHP, to redirect both domains to different locations?

For a single domain redirect, I used this so far:

Code: Select all

<?php
header("Location: v/index.jsp"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

I tried this for multiple domain redirect, but it doesn't work:

Code: Select all

<?php
header("Location: http://domain1.com" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/dir/file.ext" . $relative_url);

header("Location: http://domain2.com" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/dir/file.ext" . $relative_url);
?>

Any help greatly appreciated.

Thanks.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

htaccess

Post by Jaxolotl »

didn't you try to use an .htaccess file with some rules?
or you want to check the incoming trafic before redirect it?
some2
Forum Newbie
Posts: 5
Joined: Wed Nov 29, 2006 9:38 am

Post by some2 »

Using htaccess (on windows) would require additional settings, I believe. But yes, I want to log all incoming traffic.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

headers

Post by Jaxolotl »

the script you write won't work (as I know) because you can send only one Header information at the time, you may write a kind of condition to do what you want.
something like this

Code: Select all

<?php
if(MY CONDITION){
header("Location: http://domain1.com" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/dir/file.ext" . $relative_url);
}
elseif(SECOND CONDITION){
header("Location: http://domain2.com" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/dir/file.ext" . $relative_url);
}
else{
// DO MY DEFAULT OPTION or send the default header information
}
?>
if I'm on error please correct me, got to go now but i'll be back in some hours
some2
Forum Newbie
Posts: 5
Joined: Wed Nov 29, 2006 9:38 am

Post by some2 »

doesn't seem to work, both domains now redirect to a blank page. :(
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Multiple domain redirect

Post by timvw »

some2 wrote: I've a domain working on Apache/2.0.59 (Win32) mod_jk/1.2.18 PHP/4.4.4 setup. It works fine, but I recently configured another domain on the same server. Nameservers and everything is fine but the new domain points to the location of old domain.

Is there any way, using PHP, to redirect both domains to different locations?
Why don't you configure two virtual hosts?
NameVirtualHost *

<VirtualHost domain1:80>
DocumentRoot D:/websites/domain1
</VirtualHost>

<VirtualHost domain2:80>
DocumentRoot D:/websites/domain2
</VirtualHost>
some2
Forum Newbie
Posts: 5
Joined: Wed Nov 29, 2006 9:38 am

Post by some2 »

Doesn't work either. Both domains still point to the same location. :(
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Ok, let's start from the beginning.

How did you configure the two domains? (dns configuration, apache configuration)
some2
Forum Newbie
Posts: 5
Joined: Wed Nov 29, 2006 9:38 am

Post by some2 »

DNS Configuration:

ns8.domain1.com = xx.xx.xx.89

ns1.domain2.com = xx.xx.xx.90
ns2.domain2.com = xx.xx.xx.91

Apache configuration:

Here's the httpd.conf

Redirection:

This is the index.php in the apache root:

Code: Select all

<?php
header("Location: v/index.jsp"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>
With this, both domains currently redirect to the same jsp application (c:\www\tomcat5\webapps\v\index.jsp) whereas, the domain2.com should redirect to a different location (c:\www\tomcat5\webapps\xyz\index.jsp)


Note: I'm on windows so htaccess doesn't work, I already checked.
Post Reply