core-bundle/src/Resources/contao/classes/Frontend.php
public static function getRootPageFromUrl()
	{
		$host = Environment::get('host');
		$logger = System::getContainer()->get('monolog.logger.contao');
		$accept_language = Environment::get('httpAcceptLanguage');

		// The language is set in the URL
		if (!empty($_GET['language']) && Config::get('addLanguageToUrl'))
		{
			$strUri = Environment::get('url') . '/' . Input::get('language') . '/';
			$strError = 'No root page found (host "' . $host . '", language "'. Input::get('language') .'")';
		}

		// No language given
		else
		{
			// Always load the language fall back root if "doNotRedirectEmpty" is enabled
			if (Config::get('addLanguageToUrl') && Config::get('doNotRedirectEmpty'))
			{
				$accept_language = '-';
			}

			$strUri = Environment::get('url') . '/';
			$strError = 'No root page found (host "' . Environment::get('host') . '", languages "' . implode(', ', Environment::get('httpAcceptLanguage')) . '")';
		}

		try
		{
			$objRequest = Request::create($strUri);
			$objRequest->headers->set('Accept-Language', $accept_language);

			$arrParameters = System::getContainer()->get('contao.routing.nested_matcher')->matchRequest($objRequest);
			$objRootPage = $arrParameters['pageModel'] ?? null;

			if (!$objRootPage instanceof PageModel)
			{
				throw new MissingMandatoryParametersException('Every Contao route must have a "pageModel" parameter');
			}
		}
		catch (RoutingExceptionInterface $exception)
		{
			$logger->log(LogLevel::ERROR, $strError, array('contao' => new ContaoContext(__METHOD__, 'ERROR')));

			throw new NoRootPageFoundException('No root page found', 0, $exception);
		}

		// Redirect to the website root or language root (e.g. en/)
		if (Environment::get('relativeRequest') == '')
		{
			if (Config::get('addLanguageToUrl') && !Config::get('doNotRedirectEmpty'))
			{
				$arrParams = array('_locale' => $objRootPage->language);

				$strUrl = System::getContainer()->get('router')->generate('contao_index', $arrParams);
				$strUrl = substr($strUrl, \strlen(Environment::get('path')) + 1);

				static::redirect($strUrl, 301);
			}

			// Redirect if the page alias is not "index" or "/" (see #8498, #8560 and #1210)
			elseif ($objRootPage->type !== 'root' && !\in_array($objRootPage->alias, array('index', '/')))
			{
				static::redirect($objRootPage->getAbsoluteUrl(), 302);
			}
		}

		if ($objRootPage->type != 'root')
		{
			return PageModel::findByPk($objRootPage->rootId);
		}

		return $objRootPage;
	}
public static function getRootPageFromUrl()
	{
		$host = Environment::get('host');
		$logger = System::getContainer()->get('monolog.logger.contao');
		$accept_language = Environment::get('httpAcceptLanguage');

		// The language is set in the URL
		if (!empty($_GET['language']) && Config::get('addLanguageToUrl'))
		{
			$strUri = Environment::get('url') . '/' . Input::get('language') . '/';
			$strError = 'No root page found (host "' . $host . '", language "'. Input::get('language') .'")';
		}

		// No language given
		else
		{
			// Always load the language fall back root if "doNotRedirectEmpty" is enabled
			if (Config::get('addLanguageToUrl') && Config::get('doNotRedirectEmpty'))
			{
				$accept_language = '-';
			}

			$strUri = Environment::get('url') . '/';
			$strError = 'No root page found (host "' . Environment::get('host') . '", languages "' . implode(', ', Environment::get('httpAcceptLanguage')) . '")';
		}

		try
		{
			$objRequest = Request::create($strUri);
			$objRequest->headers->set('Accept-Language', $accept_language);

			$arrParameters = System::getContainer()->get('contao.routing.nested_matcher')->matchRequest($objRequest);
			$objRootPage = $arrParameters['pageModel'] ?? null;

			if (!$objRootPage instanceof PageModel)
			{
				throw new MissingMandatoryParametersException('Every Contao route must have a "pageModel" parameter');
			}
		}
		catch (RoutingExceptionInterface $exception)
		{
			$logger->log(LogLevel::ERROR, $strError, array('contao' => new ContaoContext(__METHOD__, 'ERROR')));

			throw new NoRootPageFoundException('No root page found', 0, $exception);
		}

		// Redirect to the website root or language root (e.g. en/)
		if (Environment::get('relativeRequest') == '')
		{
			if (Config::get('addLanguageToUrl') && !Config::get('doNotRedirectEmpty'))
			{
				$arrParams = array('_locale' => $objRootPage->language);

				$strUrl = System::getContainer()->get('router')->generate('contao_index', $arrParams);
				$strUrl = substr($strUrl, \strlen(Environment::get('path')) + 1);

				static::redirect($strUrl, 301);
			}

			// Redirect if the page alias is not "index" or "/" (see #8498, #8560 and #1210)
			elseif ($objRootPage->type !== 'root' && !\in_array($objRootPage->alias, array('index', '/')))
			{
				static::redirect($objRootPage->getAbsoluteUrl(), 302);
			}
		}

		if ($objRootPage->type != 'root')
		{
			return PageModel::findByPk($objRootPage->rootId);
		}

		return $objRootPage;
	}
Try to find a root page based on language and URL

@return PageModel