Welcome to WebmasterWorld Guest from 54.166.89.187

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Detecting which domain user is using on my website - not possible?

Multiple domains pointing to same website

     
2:22 pm on Sep 23, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 0


Hello,

I posted this question in another busy forum but noone can answer my question.

The problem.

I have a website, lets say, mydomain.com with multiple mirror domains. Multiple domains point to the same website. I need to display different content/language depending of the domain which user is using to enter my website.

Some time ago I was thinking that it's very easy:

$userhost = $_SERVER["SERVER_NAME"];

or
$userhost = $_SERVER["HTTP_HOST"]; 

or
$userhost = getenv("HTTP_HOST");


Yes, it works, but not always. In some cases $userhost is something like that:

domain
domai


Questions

1) Does it mean that there is no way how to detect the domain in php so that it works 100% always?

2) Can you show an example how can I enter website www.mydomain.com telling php that my $userhost something different as www.mydomain.com

Thank you.
9:59 am on Sept 24, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


How do those mirror websites transfer traffic to you ? they use a CNAME ? or redirects ? you can add an identification parameter to the redirect url if that is the case.

if mirror1.com redirects to mydomain.com then can it be changed to mydomain.com/?d=1 etc?

and there you can pick up the idetification, secondly i believe domain host should always work unless there is some special case i dont know about.

Ofcourse there are many more ways too.
2:37 pm on Sept 24, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 0


Anyango, thank you for your reply.

No, there are no CNAME or redirects.

All domains which point to the same website are added as server alias in Apache virtual host file:

ServerAlias domain.es domain.de domain.co.uk
9:48 pm on Sept 25, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 0


So, can anyone show me an example how can I enter website www.mydomain.com telling php that HTTP_HOST is something different than www.mydomain.com ?

I just wonder how is it possible.

Thanks.
4:00 pm on Sept 26, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


Just setup an array of the domains you want to use and they are setup on your host, ex:

$domain_array = array(
'www.example.com',
'sub1.example.com',
'sub2.example.com'
);

// Validate host
$host = $_SERVER["HTTP_HOST"];
if( !in_array($host, $domain_array) ) {
header("HTTP/1.1 301");
header("Location: http://www.example.com");
exit();
}

// Various themes domain dependent
$theme_array = array(
www.example.com' => array(
'template' => 'www',
'language' => 'english'
),
'sub1.example.com' => array(
'template' => 'sub1',
'language' => 'spanish'
),
);

// get current domain parameters
$theme_parameters = $theme_array[$host];

.........

So in the end the parameters loaded can be used for the domain the request is for. You don't need to pass any parameters in the urls and mess the links.
10:55 am on Oct 23, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 20, 2008
posts:116
votes: 0


This is what I found in php.net:

SERVER_NAME is the name defined in the apache configuration


If it's true, I can set up separate virtual hosts for each domain with the same document root.

Then

$servername = getenv("SERVER_NAME");


will show information from Apache configuration file and not from headers. Is it true? If yes, it's a good solution how I can detect which domain is being used by user.