public function urlRedirectAction(Request $request, $path, $permanent = false, $scheme = null, $httpPort = null, $httpsPort = null)
    {
        /** @var ConfigInterface $config */
        $config = $this->container->get('config');

        if ('' == $path) {
            throw new HttpException($permanent ? 410 : 404);
        }

        $statusCode = $permanent ? 301 : 302;
        // redirect if the path is a full URL

        if (parse_url($path, PHP_URL_SCHEME)) {
            return new RedirectResponse($path, $statusCode);
        }

        if (null === $scheme) {
            $scheme = $request->getScheme();
        }

        $qs = $request->getQueryString();

        if ($qs) {

            if (strpos($path, '?') === false) {
                $qs = '?'.$qs;
            }
            else {
                $qs = '&'.$qs;
            }
        }
        $port = '';

        if ('http' === $scheme) {

            if (null === $httpPort) {

                if ('http' === $request->getScheme()) {
                    $httpPort = $request->getPort();
                }
                elseif ($config->has('request.http_port')) {
                    $httpPort = $config->get('request.http_port');
                }
            }

            if (null !== $httpPort && 80 != $httpPort) {
                $port = ":$httpPort";
            }
        }
        elseif ('https' === $scheme) {

            if (null === $httpsPort) {

                if ('https' === $request->getScheme()) {
                    $httpsPort = $request->getPort();
                }
                elseif ($config->has('request.https_port')) {
                    $httpsPort = $config->get('request.https_port');
                }
            }

            if (null !== $httpsPort && 443 != $httpsPort) {
                $port = ":$httpsPort";
            }
        }

        $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$path.$qs;

        return new RedirectResponse($url, $statusCode);
    }