How to get app domain name? (I mean base url without protocol http://
or https://
)
So if app is installed on 'http://sub.example.com/app', I want to get 'sub.example.com'.
There is the PHP global $_SERVER['HTTP_HOST'] - this will return the domain without protocol, but won't give you a sub-directory, e.g. if your app is at www.domain.com/myapp/app.
Assuming you are referring to a CakePHP app (given the tag on the question), you can use the following constant upto version 2.4:
FULL_BASE_URL
In version 2.4 you can use:
Router::fullbaseUrl()
This returns the base URL with http:// or https://. You can then do a regex replace to get rid of that part.
Try:
function replace_http($url) {
$pattern = "https{0,1}:\/{2}";
return preg_replace($pattern, "", $url);
}
$baseUrl = replace_http(Router::fullBaseUrl());
I use this in my config.php/bootstrap.php which is located in config folder to get url app domain with folder
$_SERVER['HTTP_HOST'].dirname(dirname(dirname($_SERVER['PHP_SELF'])));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With