Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get domain name (base url without protocol)

Tags:

cakephp

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

like image 601
mrdaliri Avatar asked Feb 11 '23 19:02

mrdaliri


2 Answers

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());
like image 79
gazareth Avatar answered Mar 16 '23 06:03

gazareth


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']))); 
like image 38
Abhishek Avatar answered Mar 16 '23 04:03

Abhishek