Pimcore\Controller\Router\Route\Frontend::checkForRedirect PHP Method

checkForRedirect() protected method

protected checkForRedirect ( $matchRequestUri, boolean $override = false )
$matchRequestUri
$override boolean
    protected function checkForRedirect($matchRequestUri, $override = false)
    {
        // not for admin requests
        if (Tool::isFrontentRequestByAdmin()) {
            return;
        }
        try {
            $front = \Zend_Controller_Front::getInstance();
            $config = Config::getSystemConfig();
            // get current site if available
            $sourceSite = null;
            if (Site::isSiteRequest()) {
                $sourceSite = Site::getCurrentSite();
            }
            $cacheKey = "system_route_redirect";
            if (empty($this->redirects) && !($this->redirects = Cache::load($cacheKey))) {
                $list = new Redirect\Listing();
                $list->setCondition("active = 1");
                $list->setOrder("DESC");
                $list->setOrderKey("priority");
                $this->redirects = $list->load();
                Cache::save($this->redirects, $cacheKey, ["system", "redirect", "route"], null, 998);
            }
            $requestScheme = Tool::getRequestScheme();
            $matchUrl = Tool::getHostUrl() . $matchRequestUri;
            if (!empty($_SERVER["QUERY_STRING"])) {
                $matchUrl .= "?" . $_SERVER["QUERY_STRING"];
            }
            foreach ($this->redirects as $redirect) {
                $matchAgainst = $matchRequestUri;
                if ($redirect->getSourceEntireUrl()) {
                    $matchAgainst = $matchUrl;
                }
                // if override is true the priority has to be 99 which means that overriding is ok
                if (!$override || $override && $redirect->getPriority() == 99) {
                    if (@preg_match($redirect->getSource(), $matchAgainst, $matches)) {
                        // check for a site
                        if ($redirect->getSourceSite() || $sourceSite) {
                            if (!$sourceSite || $sourceSite->getId() != $redirect->getSourceSite()) {
                                continue;
                            }
                        }
                        array_shift($matches);
                        $target = $redirect->getTarget();
                        if (is_numeric($target)) {
                            $d = Document::getById($target);
                            if ($d instanceof Document\Page || $d instanceof Document\Link || $d instanceof Document\Hardlink) {
                                $target = $d->getFullPath();
                            } else {
                                Logger::error("Target of redirect no found (Document-ID: " . $target . ")!");
                                continue;
                            }
                        }
                        // support for pcre backreferences
                        $url = replace_pcre_backreferences($target, $matches);
                        if ($redirect->getTargetSite() && !preg_match("@http(s)?://@i", $url)) {
                            try {
                                $targetSite = Site::getById($redirect->getTargetSite());
                                // if the target site is specified and and the target-path is starting at root (not absolute to site)
                                // the root-path will be replaced so that the page can be shown
                                $url = preg_replace("@^" . $targetSite->getRootPath() . "/@", "/", $url);
                                $url = $requestScheme . "://" . $targetSite->getMainDomain() . $url;
                            } catch (\Exception $e) {
                                Logger::error("Site with ID " . $redirect->getTargetSite() . " not found.");
                                continue;
                            }
                        } elseif (!preg_match("@http(s)?://@i", $url) && $config->general->domain && $redirect->getSourceEntireUrl()) {
                            // prepend the host and scheme to avoid infinite loops when using "domain" redirects
                            $url = ($front->getRequest()->isSecure() ? "https" : "http") . "://" . $config->general->domain . $url;
                        }
                        // pass-through parameters if specified
                        $queryString = $_SERVER["QUERY_STRING"];
                        if ($redirect->getPassThroughParameters() && !empty($queryString)) {
                            $glue = "?";
                            if (strpos($url, "?")) {
                                $glue = "&";
                            }
                            $url .= $glue;
                            $url .= $queryString;
                        }
                        header($redirect->getHttpStatus());
                        header("Location: " . $url, true, $redirect->getStatusCode());
                        // log all redirects to the redirect log
                        \Pimcore\Log\Simple::log("redirect", Tool::getAnonymizedClientIp() . " \t Custom-Redirect ID: " . $redirect->getId() . " , Source: " . $_SERVER["REQUEST_URI"] . " -> " . $url);
                        exit;
                    }
                }
            }
        } catch (\Exception $e) {
            // no suitable route found
        }
    }