Use PSR-7 to generate the redirect to the canonical domain
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 20 Jun 2022 15:10:31 +0000 (17:10 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 20 Jun 2022 17:07:12 +0000 (19:07 +0200)
wcfsetup/install/files/lib/system/request/RequestHandler.class.php

index 1211fabb600f3d8e4e5a6ef2ff2e5736e6af2868..fa1516564ec5b027d3fa535b5e0e9ec64cb2d331 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\system\request;
 
+use Laminas\Diactoros\Response\RedirectResponse;
 use Laminas\Diactoros\ServerRequestFactory;
 use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
 use Psr\Http\Message\RequestInterface;
@@ -22,7 +23,6 @@ use wcf\system\exception\NamedUserException;
 use wcf\system\exception\SystemException;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
-use wcf\util\FileUtil;
 use wcf\util\HeaderUtil;
 
 /**
@@ -145,20 +145,13 @@ final class RequestHandler extends SingletonFactory
 
                 // check if accessing from the wrong domain (e.g. "www." omitted but domain was configured with)
                 $domainName = ApplicationHandler::getInstance()->getDomainName();
-                if ($domainName !== $_SERVER['HTTP_HOST']) {
-                    // build URL, e.g. http://example.net/forum/
-                    $url = FileUtil::addTrailingSlash(
-                        RouteHandler::getProtocol() . $domainName . RouteHandler::getPath()
-                    );
-
-                    // query string, e.g. ?foo=bar
-                    if (!empty($_SERVER['QUERY_STRING'])) {
-                        $url .= '?' . $_SERVER['QUERY_STRING'];
-                    }
+                if ($domainName !== $psrRequest->getUri()->getHost()) {
+                    $targetUrl = $psrRequest->getUri()->withHost($domainName);
 
-                    HeaderUtil::redirect($url, true, false);
-
-                    exit;
+                    return new RedirectResponse(
+                        $targetUrl,
+                        301
+                    );
                 }
             }