如何获取网站的根url


How do I get the root url of the site?

这可能是一个微不足道的问题,但我正在寻找一种方法来表示获取网站url的根,例如:http://localhost/some/folder/containing/something/here/or/there应该返回http://localhost/

我知道有$_SERVER['DOCUMENT_ROOT'],但那不是我想要的。

我相信这很容易,但我一直在读:这篇文章试图弄清楚我应该使用或调用什么。

想法?

与此相关的另一个问题是,答案会在http://subsite.localhost/some/folder/containing/something/here/or/there这样的网站上工作吗?所以我的最终结果是http://subsite.localhost/

$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

如果您对当前脚本的方案和宿主感兴趣。根据web服务器配置(反向代理、重写);当前";可能不是你在浏览器中看到的,但这是PHP认为它正在处理的问题。

否则,如果您手头有一个URL,您需要parse_URL()的根,正如前面所建议的那样。例如

$parsedUrl = parse_url('http://localhost/some/folder/containing/something/here/or/there');
$root = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . '/';

如果您对路径之前的其他URL组件(例如凭据)也感兴趣,您也可以在完整的URL上使用substr(),以"开头;路径";作为停止位置,例如

$url = 'http://user:pass@localhost:80/some/folder/containing/something/here/or/there';
$parsedUrl = parse_url($url);
$root = substr($url, 0, strpos($url, $parsedUrl['path'], strlen($parsedUrl['scheme'] . '://'))) . '/';//gives 'http://user:pass@localhost:80/'

注意:strpos()中的偏移量用于处理URL可能已经是根的情况。

另一种简单的方法:

<?php
$hostname = getenv('HTTP_HOST');
echo $hostname;

getenv

(PHP 4,PHP 5)

getenv--获取环境变量的值

此PHP函数返回完整路径的真实URL。

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;
}

调用此文件中的pathUrl:http://example.com/shop/index.php

#index.php
echo pathUrl();
//http://example.com/shop/

使用别名:http://example.com/alias-name/shop/index.php

#index.php
echo pathUrl();
//http://example.com/alias-name/shop/

对于子目录:http://example.com/alias-name/shop/inc/config.php

#config.php
echo pathUrl(__DIR__ . '/../');
//http://example.com/alias-name/shop/

您可以在define.php include上使用Define保存,以便下次用于其他项目这是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);

您可以调试以查看结果

您可以preg_split第一个/字符处的URL:

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]

函数rootUrl($url)返回给定字符串$url在第一个/字符之前的部分,如果第一部分不为空,则返回后面的/。对于以/(相对URL)开头的URL,将返回一个空字符串。