From 100638fb6bae6980424c1c0d5a492f2530870b80 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 19 Sep 2024 16:54:47 -0400 Subject: [PATCH] chore: Maintain backwards compatibility for redirects with site prefixes in the path ([#288](https://github.com/nystudio107/craft-retour/issues/288)) --- src/gql/resolvers/RetourResolver.php | 19 +++++++++---------- src/services/Redirects.php | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/gql/resolvers/RetourResolver.php b/src/gql/resolvers/RetourResolver.php index a41aaca..c39dce3 100644 --- a/src/gql/resolvers/RetourResolver.php +++ b/src/gql/resolvers/RetourResolver.php @@ -76,16 +76,15 @@ public static function resolve(mixed $source, array $arguments, mixed $context, Retour::$plugin->statistics->incrementStatistics($uri, false, $siteId); } } - if ($redirect !== null) { - $dest = $redirect['redirectDestUrl']; - // If this isn't an absolute URL, make it one based on the appropriate site - if (!UrlHelper::isAbsoluteUrl($dest)) { - try { - $dest = UrlHelper::siteUrl($dest, null, null, $siteId); - $dest = parse_url($dest, PHP_URL_PATH); - } catch (Throwable $e) { - // That's ok - } + if ($redirect !== null && isset($redirect['redirectDestUrl'])) { + $path = $redirect['redirectDestUrl']; + // Combine the URL and path together, merging them as appropriate + try { + $dest = UrlHelper::siteUrl('/', null, null, $siteId); + $dest = UrlHelper::mergeUrlWithPath($dest, $path); + $dest = parse_url($dest, PHP_URL_PATH); + } catch (Throwable $e) { + // That's ok } $redirect['redirectDestUrl'] = $dest; } diff --git a/src/services/Redirects.php b/src/services/Redirects.php index 42d274c..fba1dbb 100644 --- a/src/services/Redirects.php +++ b/src/services/Redirects.php @@ -819,9 +819,21 @@ public function doRedirect(string $fullUrl, string $pathOnly, ?array $redirect): if ($redirect !== null) { // Figure out what type of source matching was done $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly'; + $dest = $redirect['redirectDestUrl']; + $path = $redirect['redirectDestUrl']; switch ($redirectSrcMatch) { case 'pathonly': $url = $pathOnly; + try { + $siteId = $redirect['siteId'] ?? null; + if ($siteId !== null) { + $siteId = (int)$siteId; + } + $dest = UrlHelper::siteUrl('/', null, null, $siteId); + $dest = UrlHelper::mergeUrlWithPath($dest, $path); + $dest = parse_url($dest, PHP_URL_PATH); + } catch (\yii\base\Exception $e) { + } break; case 'fullurl': $url = $fullUrl; @@ -830,9 +842,8 @@ public function doRedirect(string $fullUrl, string $pathOnly, ?array $redirect): $url = $pathOnly; break; } - $dest = $redirect['redirectDestUrl']; - // If this isn't an absolute URL, make it one based on the appropriate site - if (!UrlHelper::isAbsoluteUrl($dest)) { + // If this isn't a full URL, make it one based on the appropriate site + if (!UrlHelper::isFullUrl($dest)) { try { $siteId = $redirect['siteId'] ?? null; if ($siteId !== null) {