If virtualhost is well configured then SERVER_NAME
should work fine, but I believe that your case is another medium, such as proxy-reverse, or the hosts for your applications are resolved by the header Host:
in HTTP. p>
In other words, the value of SERVER_NAME
is not changed, because SERVER_
variables are usually fixed values of settings.
So to solve this you will have to use HTTP_HOST
, since variables with HTTP_
prefix are based on headers.
The SERVER_NAME
is probably not "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 correctly, example in Apache:
<VirtualHost *:80>
ServerName dominio.com
ServerAlias foo.dominio.com
ServerAlias bar.dominio.com
ServerAlias baz.dominio.com
DocumentRoot /public
</VirtualHost>
But your settings are probably "dynamic" , meaning your subdomains are solved by the header itself, so in this case the variable SERVER_NAME
is only populated with the main host alias. that the other should not exist in the settings , so the way is to use $_SERVER['HTTP_HOST']
that will get the value of Host:
, as in the example when requesting the page in your browser this will occur: p>
GET /foo/bar/baz HTTP/1.1
Host: subdominio.dominio.com
Connection: keep-alive
Then the value of Host:
that is in the example subdominio.dominio.com
, will be populated to $_SERVER['HTTP_HOST']
, thus:
$caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";
Note: I assume that $http
you already have, to find out if it's HTTP or HTTPS, if you do not have it just do this:
$http = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";