简体   繁体   English

如何获取网站的根网址?

[英]How do I get the root url of the site?

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 should return http://localhost/ 可能是一个微不足道的问题,但我正在寻找一种方法来获取网站URL的根,例如: http://localhost/some/folder/containing/something/here/or/there应返回http://localhost/

I know there is $_SERVER['DOCUMENT_ROOT'] but that's not what I want. 我知道有$_SERVER['DOCUMENT_ROOT']但这不是我想要的。

I am sure this is easy, but I have been reading: This post trying to figure out what i should use or call. 我确信这很容易,但我一直在阅读: 这篇文章试图找出我应该使用或调用的内容。

Ideas? 想法?

The other question I have, which is in relation to this one is - will what ever the answer be, work on sites like http://subsite.localhost/some/folder/containing/something/here/or/there so my end result is http://subsite.localhost/ 我所遇到的另一个问题是 - 这将是什么样的答案,在http://subsite.localhost/some/folder/containing/something/here/or/there等网站上工作,所以我的结局结果是http://subsite.localhost/

$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. 否则,就像已经建议的那样,是parse_url()。 eg 例如

$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 (eg credentials), you could also use strstr() on the full URL, with the "path" as the needle, eg 如果您还对路径之前的其他URL组件(例如凭据)感兴趣,您还可以在完整URL上使用strstr(),并将“path”作为指针,例如

$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/'

Another simple way: 另一个简单方法:

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

getenv GETENV

(PHP 4, PHP 5) (PHP 4,PHP 5)

getenv — Gets the value of an environment variable getenv - 获取环境变量的值

This PHP function returns the real URL of a full path. 此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;
}

Call of pathUrl in this file : http://example.com/shop/index.php 在此文件中调用pathUrl: 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 使用别名: 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 对于子目录: http//example.com/alias-name/shop/inc/config.php

#config.php

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

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 您可以通过Define节省define.php包括在其他项目下一次使用这个PROTOCOL DOMAIN PORT SITE_ROOTSITE 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 您可以调试以查看结果

You can preg_split the URL at the first single / -character: 您可以在第一个/ -character中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]

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. 函数rootUrl($url)返回给定字符串$url的一部分,在第一个/ -character加上尾随/如果第一部分不为空。 For URLs starting with a / (relative URLs) an empty string is returned. 对于以/ (相对URL)开头的URL,将返回空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM