Might be a trivial question, but I am looking for a way to say get the root of a site url, for example: http://localhost/some/folder/containing/something/here/or/there
Another simple way:
<?php
$hostname = getenv('HTTP_HOST');
echo $hostname;
getenv
(PHP 4, PHP 5)
getenv — Gets the value of an environment variable
You can using Define
save on define.php
include for next time use on other project
This is PROTOCOL
DOMAIN
PORT
SITE_ROOT
AND SITE PATH
**
* domain
* ex: localhost, maskphp.com, demo.maskphp.com,...
*/
define('DOMAIN', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);
/**
* protocol
* ex: http, https,...
*/
define('PROTOCOL', isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1)
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http');
/**
* port
* ex: 80, 8080,...
*/
define('PORT', $_SERVER['SERVER_PORT']);
/**
* site path
* ex: http://localhost/maskphp/ -> /maskphp/
*/
define('SITE_PATH', preg_replace('/index.php$/i', '', $_SERVER['PHP_SELF']));
/**
* site root
* ex: http://maskgroup.com, http://localhost/maskphp/,...
*/
define('SITE_ROOT', PROTOCOL . '://' . DOMAIN . (PORT === '80' ? '' : ':' . PORT) . SITE_PATH);
You can debug to see result
This PHP function returns the real URL of a full path.
function pathUrl($dir = __DIR__){
$root = "";
$dir = str_replace('\\', '/', realpath($dir));
//HTTPS or HTTP
$root .= !empty($_SERVER['HTTPS']) ? 'https' : 'http';
//HOST
$root .= '://' . $_SERVER['HTTP_HOST'];
//ALIAS
if(!empty($_SERVER['CONTEXT_PREFIX'])) {
$root .= $_SERVER['CONTEXT_PREFIX'];
$root .= substr($dir, strlen($_SERVER[ 'CONTEXT_DOCUMENT_ROOT' ]));
} else {
$root .= substr($dir, strlen($_SERVER[ 'DOCUMENT_ROOT' ]));
}
$root .= '/';
return $root;
}
Call of pathUrl in this file : http://example.com/shop/index.php
#index.php
echo pathUrl();
//http://example.com/shop/
Work with alias : http://example.com/alias-name/shop/index.php
#index.php
echo pathUrl();
//http://example.com/alias-name/shop/
For sub directory : http://example.com/alias-name/shop/inc/config.php
#config.php
echo pathUrl(__DIR__ . '/../');
//http://example.com/alias-name/shop/
You can preg_split
the URL at the first single /
-character:
function rootUrl($url) {
$urlParts = preg_split('#(?<!/)/(?!/)#', $url, 2);
return $urlParts[0] != '' ? $urlParts[0] . '/' : '';
}
// examples:
echo rootUrl('http://user@www.example.com/path/to/file?query=string');
// output: http://user@www.example.com/
echo rootUrl('https://www.example.com/');
// output: https://www.example.com/
echo rootUrl('https://www.example.com');
// output: https://www.example.com/
echo rootUrl('example.com/path/to/file');
// output: example.com/
echo rootUrl('/path/to/file');
// output: [empty]
echo rootUrl('');
// output: [empty]
The function rootUrl($url)
returns the part of the given string $url
before the first /
-character plus a trailing /
if that first part is not empty. For URLs starting with a /
(relative URLs) an empty string is returned.
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
If you're interested in the current script's scheme and host.
Otherwise, parse_url(), as already suggested. e.g.
$parsedUrl = parse_url('http://localhost/some/folder/containing/something/here/or/there');
$root = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . '/';
If you're also interested in other URL components prior to the path (e.g. credentials), you could also use strstr() on the full URL, with the "path" as the needle, e.g.
$url = 'http://user:pass@localhost:80/some/folder/containing/something/here/or/there';
$parsedUrl = parse_url($url);
$root = strstr($url, $parsedUrl['path'], true) . '/';//gives 'http://user:pass@localhost:80/'