redirect code - multiple domain names

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
impactweb501
Forum Newbie
Posts: 1
Joined: Tue Jul 13, 2010 7:00 pm

redirect code - multiple domain names

Post 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');
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: redirect code - multiple domain names

Post 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();

}
Post Reply