homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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)


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"];
$userhost = $_SERVER["HTTP_HOST"];
$userhost = getenv("HTTP_HOST");

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



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 Sep 24, 2010 (gmt 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 Sep 24, 2010 (gmt 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 Sep 25, 2010 (gmt 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.



 4:00 pm on Sep 26, 2010 (gmt 0)

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

$domain_array = array(

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

// 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)

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.


$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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved