Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Request.php

function Request::prepareBaseUrl

Prepares the base URL.

1 call to Request::prepareBaseUrl()
Request::getBaseUrlReal in vendor/symfony/http-foundation/Request.php
Returns the real base URL received by the webserver from which this request is executed. The URL does not include trusted reverse proxy prefix.

File

vendor/symfony/http-foundation/Request.php, line 1789

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

protected function prepareBaseUrl() : string {
    $filename = basename($this->server
        ->get('SCRIPT_FILENAME', ''));
    if (basename($this->server
        ->get('SCRIPT_NAME', '')) === $filename) {
        $baseUrl = $this->server
            ->get('SCRIPT_NAME');
    }
    elseif (basename($this->server
        ->get('PHP_SELF', '')) === $filename) {
        $baseUrl = $this->server
            ->get('PHP_SELF');
    }
    elseif (basename($this->server
        ->get('ORIG_SCRIPT_NAME', '')) === $filename) {
        $baseUrl = $this->server
            ->get('ORIG_SCRIPT_NAME');
        // 1and1 shared hosting compatibility
    }
    else {
        // Backtrack up the script_filename to find the portion matching
        // php_self
        $path = $this->server
            ->get('PHP_SELF', '');
        $file = $this->server
            ->get('SCRIPT_FILENAME', '');
        $segs = explode('/', trim($file, '/'));
        $segs = array_reverse($segs);
        $index = 0;
        $last = \count($segs);
        $baseUrl = '';
        do {
            $seg = $segs[$index];
            $baseUrl = '/' . $seg . $baseUrl;
            ++$index;
        } while ($last > $index && false !== ($pos = strpos($path, $baseUrl)) && 0 != $pos);
    }
    // Does the baseUrl have anything in common with the request_uri?
    $requestUri = $this->getRequestUri();
    if ('' !== $requestUri && '/' !== $requestUri[0]) {
        $requestUri = '/' . $requestUri;
    }
    if ($baseUrl && null !== ($prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl))) {
        // full $baseUrl matches
        return $prefix;
    }
    if ($baseUrl && null !== ($prefix = $this->getUrlencodedPrefix($requestUri, rtrim(\dirname($baseUrl), '/' . \DIRECTORY_SEPARATOR) . '/'))) {
        // directory portion of $baseUrl matches
        return rtrim($prefix, '/' . \DIRECTORY_SEPARATOR);
    }
    $truncatedRequestUri = $requestUri;
    if (false !== ($pos = strpos($requestUri, '?'))) {
        $truncatedRequestUri = substr($requestUri, 0, $pos);
    }
    $basename = basename($baseUrl ?? '');
    if (!$basename || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
        // no match whatsoever; set it blank
        return '';
    }
    // If using mod_rewrite or ISAPI_Rewrite strip the script filename
    // out of baseUrl. $pos !== 0 makes sure it is not matching a value
    // from PATH_INFO or QUERY_STRING
    if (\strlen($requestUri) >= \strlen($baseUrl) && false !== ($pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) {
        $baseUrl = substr($requestUri, 0, $pos + \strlen($baseUrl));
    }
    return rtrim($baseUrl, '/' . \DIRECTORY_SEPARATOR);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal