Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is borderline programming, but i'm posting here first.

output of PHPinfo

Apache Version  Apache/2.2.3 (CentOS)
Apache API Version  20051115
Server Administrator    root@localhost
User/Group  apacheuser(500)/500

I'm trying to find a way for PHP to retrieve the "Hostname:port" field in this area.

We have apache dump all our virtual host information into a single file, and then that single file is broken up into several files & relevant information (total hits, ect) is logged to a database. One of the problems we're having at the moment is if a domain comes in on a ServerAlias, our php software isn't able to find the database table containing their logs (because the domain is technically not correct.) However, the "Hostname:port" field in php / phpinfo(); Always retrives the information i want, which is the primary domain name.

If there is any other method PHP can gain the "ServerName" field from apache i'm unaware of please let me know, as that would be alot easier.

An Alternative approach is having a SetEnv command in every virtual host, however instead of having to go through and add a

SetEnv primarydomain

to every VirtualHost, is there a way to use a variable?

In Example:

SetEnv primarydomain %v



We solved the problem with a regular expression search & replace.

Search: ^ServerName\s\b((?=[a-z0-9-]{1,63}.)[a-z0-9]+(-[a-z0-9]+)*.)+([a-z]{2,63})\b

Replace: $&\nSetEnv\tprimarydomain\t$1$3

share|improve this question

If the standard $_SERVER['SERVER_NAME'] isnt waht you are after, have you tried $_SERVER['SERVER_SIGNATURE']?

share|improve this answer
Unfortunately both of those return the ServerAlias instead of ServerName. =/ – GruffTech Jul 23 '09 at 19:26

Do you set the ServerAdmin directive in the virtual host?

If so, I'd use $_SERVER['SERVER_ADMIN'], and take the latter part of the e-mail address after the @ symbol.


array_pop(split("@", $_SERVER['SERVER_ADMIN'], 2))'
share|improve this answer
up vote 0 down vote accepted

Problem solved with a regexp.

Search: ^ServerName\s\b((?=[a-z0-9-]{1,63}.)[a-z0-9]+(-[a-z0-9]+)*.)+([a-z]{2,63})\b

Replace: $&\nSetEnv\tprimarydomain\t$1$3

share|improve this answer
Can you explain more how you did this? – Josh K May 1 '10 at 3:03

If you're not interested in the client supplied hostnames then you might wish to use the directives:

UseCanonicalName and UseCanonicalPhysicalPort.

share|improve this answer

For the real server name (from the OS, not the virtual server) :

echo php_uname('n');

For the port :

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.