Skip to content

Commit

Permalink
chore: Maintain backwards compatibility for redirects with site prefi…
Browse files Browse the repository at this point in the history
…xes in the path ([#288](#288))
  • Loading branch information
khalwat committed Sep 19, 2024
1 parent d24beb4 commit 100638f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
19 changes: 9 additions & 10 deletions src/gql/resolvers/RetourResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 89 in src/gql/resolvers/RetourResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $dest might not be defined.
}
Expand Down
17 changes: 14 additions & 3 deletions src/services/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 100638f

Please sign in to comment.