Page 1 of 1

redirect code - multiple domain names

Posted: Tue Jul 13, 2010 7:12 pm
by impactweb501
Hi,
I am fluent in ASP but very limited in PHP!

I have one account (http://www.domainone.com.au) with three domain aliases and on the root index.php page - I would like the domain name to be detected and redirected to a specific page with the correct domain name in the title bar. (The domains used are just examples.)

Can anyone provide help or any ideas? Thanks!

Here is the proposed index.php code:

<?
$aa = 'www.domainone.com.au';
$bb = 'www.domainone.com';
$cc = 'www.domaintwo.com.au';
$dd = 'www.domainthree.com.au';

$url = $_SERVER['SERVER_NAME'];

if ($url = $aa)
{
header('Location: http://www.domainone.com.au/folderone/index.php');
}
else
if ($url = $bb)
{
header('Location: http://www.domainone.com/folderone/index.php');
}
else
if ($url = $cc)
{
header('Location: http://www.domaintwo.com.au/foldertwo/index.php');
}
else
if ($url = $dd)
{
header('Location: http://www.domainthree.com.au/folderthree/index.php');
}
?>

Re: redirect code - multiple domain names

Posted: Tue Jul 13, 2010 7:33 pm
by John Cartwright
= is the assignment operator
== is the comparison operator
=== is the type sensitive comparison operator

So basically, you should be using the comparison operator instead of the assignment operator.

Alternatively, you could simplify your code by using a lookup table of some sort:

Code: Select all

$domains = array(
   'www.domainone.com.au' => 'http://www.domainone.com.au/folderone/index.php',
   'www.domainone.com' => 'http://www.domainone.com/folderone/index.php'
   //etc
);

if (array_key_exists($_SERVER['SERVER_NAME'], $domains)) {
   
   header('Location: '. $domains[$_SERVER['SERVER_NAME']]);
   exit();

}