src/Tomahawk/Routing/Controller/RedirectController.php
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);
    }
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);
    }
Redirects to a URL.

The response status code is 302 if the permanent parameter is false (default),
and 301 if the redirection is permanent.

In case the path is empty, the status code will be 404 when permanent is false
and 410 otherwise.

@param Request $request
@param $path
@param bool|false $permanent
@param null $scheme
@param null $httpPort
@param null $httpsPort
@return RedirectResponse
@throws HttpException