Prepare's the request path for a URL rewrite or the target path for a 301 redirect.

@return string The request path
438605
protected function prepareRequestPath()
    {

        // load the category URL suffix to use
        $urlSuffix = $this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_CATEGORY_URL_SUFFIX, '.html');

        // prepare and return the category URL
        return sprintf('%s%s', $this->getValue(ColumnKeys::URL_PATH), $urlSuffix);
    }
483794
public static function path()
    {
        $url = '';

        // Get the rest of the URL
        if (!Arr::key('REQUEST_URI', $_SERVER)) {
            // Microsoft IIS doesn't set REQUEST_URI by default
            if ($queryString = Arr::key('QUERY_STRING', $_SERVER, true)) {
                $url .= '?' . $queryString;
            }
        } else {
            $url .= $_SERVER['REQUEST_URI'];
        }

        return $url ?: null;
    }
1265305
public function buildPath() : string
    {
        return self::buildRepositoryPath(
            dirname($this->path),
            $this->domain,
            $this->language
        );
    }
903442
private function getPathInfo()
    {
        $requestURI = !empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';

        $this->validateRequestURI($requestURI);

        if (empty($requestURI)) {
            return;
        }

        $request = $requestURI;

        $this->fullRequest = $this->currentRequest = $request;
        $this->addDebugMessage("Full request " . $this->fullRequest);

        $secure = !empty($_SERVER['HTTPS']);
        $this->addDebugMessage($secure ? "Secure routing." : "Non-secure routing.");

        $this->baseHREF = "http" . ($secure ? 's' : '') . '://' . $_SERVER['SERVER_NAME'];

        $this->currentHREF = $this->baseHREF . $request;

        $queryStringIndex = strpos($request, '?');

        $startsWithSlash = ($request[0] == '/' ? 1 : 0);

        if ($queryStringIndex !== false) {
            $this->currentRequest = substr($request, 0, $queryStringIndex);

            $request = substr($request, $startsWithSlash, $queryStringIndex - 1);
        } else {
            $request = substr($request, $startsWithSlash);
        }

        if (!empty($request)) {
            $this->segments = explode('/', $request);
        }

        $this->isHomepage = (empty($request) || $request == '/');

        $this->checkForTrailingSlashRedirect();
    }
211315
private function getCurrentUrlPath()
    {
        if ($this->path) {
            return $this->path;
        }

        /**
         * check if the path is empty otherwise
         * set it with the server variable and if there's no server variable set it as the default delimiter [/] the root
         */
        $this->path = !empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
        $this->urlPart = $this->parsePath($this->path);

        return $this->path;
    }
298832
public function getRedirectUri()
    {
        // Force `addTrailingSlashesToUrls` to `false` while we generate the redirectUri
        $addTrailingSlashesToUrls = \Craft\craft()->config->get('addTrailingSlashesToUrls');
        \Craft\craft()->config->set('addTrailingSlashesToUrls', false);

        $redirectUri = \Craft\UrlHelper::getActionUrl('oauth/connect');

        // Set `addTrailingSlashesToUrls` back to its original value
        \Craft\craft()->config->set('addTrailingSlashesToUrls', $addTrailingSlashesToUrls);

        // We don't want the CP trigger showing in the action URL.
        $redirectUri =  str_replace(\Craft\craft()->config->get('cpTrigger').'/', '', $redirectUri);

        OauthPlugin::log('Redirect URI: '. $redirectUri, LogLevel::Info);

        return $redirectUri;
    }
1171314
public function getRequestTarget()
    {
        if ($this->requestTarget !== null) {
            return $this->requestTarget;
        }

        if (!$this->uri) {
            return '/';
        }

        $requestTarget = $this->getUri()->getPath();
        if ($query = $this->getUri()->getQuery()) {
            $requestTarget .= sprintf('?%s', $query);
        }

        return empty($requestTarget) ? '/' : $requestTarget;
    }
869812
public function getRedirectReferer()
    {
        $redirect = $this->parameters->get('redirect');
        $referer = $this->request->headers->get('referer');

        if (!is_array($redirect) || empty($redirect['referer'])) {
            return $referer;
        }

        if ($redirect['referer'] === true) {
            return $referer;
        }

        return $redirect['referer'];
    }
968481
protected function resolveRedirectRoute()
    {
        if (empty($this->redirectRoute)) return;

        $route = $this->system->router()->getRouteByName($this->redirectRoute);

        if (empty($route))
        {
            $this->setNotReady("Unable to find route " . $this->redirectRoute);
            return;
        }

        $this->redirectURL = $route->getPath($this->redirectParameters);
    }
1043001
private function searchRedirect(): void
    {
        // calculated depend of language
        $pathway = $this->getPathInfo();
        /** @var array $routing */
        $routing = App::$Properties->getAll('Routing');

        if (!Any::isArray($routing) || !isset($routing['Redirect']) || !Any::isArray($routing['Redirect'])) {
            return;
        }

        // check if source uri is key in redirect target map
        if (array_key_exists($pathway, $routing['Redirect'])) {
            $target = $this->getSchemeAndHttpHost(); // . $this->getBasePath() . '/' . rtrim($routing['Redirect'][$pathway], '/');
            if ($this->getBasePath() !== null && !Str::likeEmpty($this->getBasePath())) {
                $target .= '/' . $this->getBasePath();
            }
            $target .= rtrim($routing['Redirect'][$pathway], '/');
            $redirect = new RedirectResponse($target);
            $redirect->send();
            exit();
        }
    }
1228018
public function getRouteURL()
    {
        if (empty($this->routeUrl)) {
            $param = trim(Server::get('REQUEST_URI'), '/');

            // strip base directory from request url
            $base_dir = $this->getBaseDir();
            $param = substr($param, strlen($base_dir));

            // Strip query string (?a=b) from Request Url
            $path = trim(explode('?', $param)[0], '/');
            $this->routeUrl = strtolower($path);

            if (empty($this->routeUrl)) {
                $this->routeUrl = '/';
            }
        }

        return $this->routeUrl;
    }
127768
public static function redirectToDefaultLanguage()
    {
        $getCurrentPath = Request::path();
        $explodePath = explode('/', $getCurrentPath);

        //only proceed if there is a language found in first path and that's default language
        if(App::getLocale() == Language::getDefault("slug")) {
            $countPaths = count($explodePath);
            $getLanguage = Language::findBySlug($explodePath[0]);

            if($getLanguage) {
                if($getLanguage->isDefault) {
                    if($countPaths >= 2 && $explodePath[0] && $explodePath[1]) {
                        $urlAfterLanguage = substr($getCurrentPath, 3);
                        Redirect::to($urlAfterLanguage, 301)->send();
                    }
                    //that's shall be default language
                    else if($countPaths == 1 && $explodePath[0]) {
                        Redirect::to('/', 301)->send();
                    }
                }
            }
        }
    }
343537
protected function prepareRequestUri()
    {
        $requestUri = '';

        if ($this->headers->has('X_REWRITE_URL') && false !== stripos(PHP_OS, 'WIN')) {
            // check this first so IIS will catch
            $requestUri = $this->headers->get('X_REWRITE_URL');
        } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') {
            // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
            $requestUri = $this->server->get('UNENCODED_URL');
        } elseif ($this->server->has('REQUEST_URI')) {
            $requestUri = $this->server->get('REQUEST_URI');
            // HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
            $schemeAndHttpHost = $this->getScheme().'://'.$this->getHttpHost();
            if (strpos($requestUri, $schemeAndHttpHost) === 0) {
                $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
            }
        } elseif ($this->server->has('ORIG_PATH_INFO')) {
            // IIS 5.0, PHP as CGI
            $requestUri = $this->server->get('ORIG_PATH_INFO');
            if ($this->server->get('QUERY_STRING')) {
                $requestUri .= '?'.$this->server->get('QUERY_STRING');
            }
        }

        return $requestUri;
    }
214875
protected function process()
    {

        // process the new URL rewrites first
        parent::process();

        // create redirect URL rewrites for the existing URL rewrites
        foreach ($this->existingUrlRewrites as $existingUrlRewrite) {
            // query whether or not 301 redirects have to be created, so don't
            // create redirects if the the rewrite history has been deactivated
            if ($this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_SAVE_REWRITES_HISTORY, true)) {
                // if the URL rewrite already IS a redirect
                if ((integer) $existingUrlRewrite[MemberNames::REDIRECT_TYPE] !== 0 &&
                    (integer) $existingUrlRewrite[MemberNames::IS_AUTOGENERATED] === 0
                ) {
                    // do NOT create another redirect
                    continue;
                }

                // load target path
                $targetPath = $this->prepareRequestPath();

                // override data with the 301 configuration
                $attr = array(
                    MemberNames::REDIRECT_TYPE    => 301,
                    MemberNames::TARGET_PATH      => $targetPath,
                    MemberNames::IS_AUTOGENERATED => 0
                );

                // merge and return the prepared URL rewrite
                $existingUrlRewrite = $this->mergeEntity($existingUrlRewrite, $attr);

                // create the URL rewrite
                $this->persistUrlRewrite($existingUrlRewrite);
            } else {
                // query whether or not the URL rewrite has to be removed
                if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) &&
                    $this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES)
                ) {
                    // delete the existing URL rewrite
                    $this->deleteUrlRewrite($existingUrlRewrite);

                    // log a message, that old URL rewrites have been cleaned-up
                    $this->getSubject()
                         ->getSystemLogger()
                         ->warning(
                             sprintf(
                                 'Cleaned-up %d URL rewrite "%s" for category with path "%s"',
                                 $existingUrlRewrite[MemberNames::REQUEST_PATH],
                                 $this->getValue(ColumnKeys::PATH)
                             )
                         );
                }
            }
        }
    }
845628
public function getPath()
    {
        $pathInfo = rawurldecode($this->getPathInfo());
        $path = '/' . trim($pathInfo, '/');

        return ($path == '/') ? '' : $path;
    }
56289
public function getUrlPath()
    {
        $sUri = $this->getServerParam('REQUEST_URI', \FILTER_SANITIZE_URL);
        if (!is_null($sUri)) {
            return parse_url($sUri, \PHP_URL_PATH);
        }
        return null;
    }
224026
public function getHostRelativeUrl()
    {
        $url = $this->getPath();
        if ($this->hasQueryString()) $url .= '?' . $this->getQueryString();
        if ($this->hasFragmentString()) $url .= '#' . $this->getFragmentString();
        return $url;
    }