If virtualhost is well configured then SERVER_NAME
should work well, but I believe your case is another way, such as proxy reverse or else the hosts for your applications are solved by header Host:
at HTTP.
That is the value of SERVER_NAME
is not changed as the variables SERVER_
are usually fixed values of configurations.
So to solve this you will have to use the HTTP_HOST
, because the variables with prefix HTTP_
are based on headers.
The SERVER_NAME
probably not this "configured" for each specific subdomain, ie the process is dynamic and not "fixed" within Virtualhost, for example if each subdomain was defined with ServerName
and ServerAlias
Virtualhost would work properly, example in Apache:
<VirtualHost *:80>
ServerName dominio.com
ServerAlias foo.dominio.com
ServerAlias bar.dominio.com
ServerAlias baz.dominio.com
DocumentRoot /public
</VirtualHost>
But probably your settings are "dynamic", i.e., your subdomains are solved by the header itself, so in this case the variable SERVER_NAME
is only populated with the main host alias, since the other shall not exist in the configurations, then the way is to use $_SERVER['HTTP_HOST']
that will take the value of Host:
, as in the example when requesting the page in your browser this will occur:
GET /foo/bar/baz HTTP/1.1
Host: subdominio.dominio.com
Connection: keep-alive
Then the value of Host:
which is in the example subdominio.dominio.com
, will be populated for the $_SERVER['HTTP_HOST']
, being like this:
$caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";
Note: I suppose the $http
you already have, to know if it is HTTP or HTTPS, if you have not yet do this only:
$http = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";
I believe the variable
$_SERVER['HTTP_HOST']
return it complete, no?– Francisco