Skip to content

Commit 0479dd3

Browse files
committed
fix: Fixed a regression that caused the redirect loop detection to no longer function ([#348](#348))
1 parent cb1cf83 commit 0479dd3

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/controllers/RedirectsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function actionSaveRedirect()
271271
}
272272
// Save the redirect
273273
$redirectConfig = $redirect->getAttributes();
274-
Retour::$plugin->redirects->saveRedirect($redirectConfig);
274+
Retour::$plugin->redirects->saveRedirect($redirectConfig, false);
275275
// Handle the case where the redirect wasn't saved because it'd create a redirect loop
276276
$testRedirectConfig = Retour::$plugin->redirects->getRedirectByRedirectSrcUrl(
277277
$redirectConfig['redirectSrcUrl'],

src/services/Redirects.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,10 @@ public function deleteShortlinkById(int $redirectId)
10861086

10871087
/**
10881088
* @param array $redirectConfig
1089-
* @return bool whether the redirect was saved or not
1089+
* @param bool $checkForRedirectLoop
1090+
* @return bool
10901091
*/
1091-
public function saveRedirect(array $redirectConfig): bool
1092+
public function saveRedirect(array $redirectConfig, bool $checkForRedirectLoop = true): bool
10921093
{
10931094
// Handle URL encoded URLs by decoding them before saving them
10941095
if (isset($redirectConfig['redirectMatchType']) && $redirectConfig['redirectMatchType'] === 'exactmatch') {
@@ -1201,28 +1202,30 @@ public function saveRedirect(array $redirectConfig): bool
12011202
return false;
12021203
}
12031204
}
1204-
// To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
1205-
$testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
1206-
$redirectConfig['redirectDestUrl'],
1207-
$redirectConfig['siteId']
1208-
);
1209-
if ($testRedirectConfig !== null) {
1210-
Craft::debug(
1211-
Craft::t(
1212-
'retour',
1213-
'Deleting redirect to prevent a loop: {redirect}',
1214-
['redirect' => print_r($testRedirectConfig, true)]
1215-
),
1216-
__METHOD__
1205+
if ($checkForRedirectLoop) {
1206+
// To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
1207+
$testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
1208+
$redirectConfig['redirectDestUrl'],
1209+
$redirectConfig['siteId']
12171210
);
1218-
// Delete the redirect that has a redirectSrcUrl the same as this record's redirectDestUrl
1219-
try {
1220-
$db->createCommand()->delete(
1221-
'{{%retour_static_redirects}}',
1222-
['id' => $testRedirectConfig['id']]
1223-
)->execute();
1224-
} catch (Exception $e) {
1225-
Craft::error($e->getMessage(), __METHOD__);
1211+
if ($testRedirectConfig !== null) {
1212+
Craft::debug(
1213+
Craft::t(
1214+
'retour',
1215+
'Deleting redirect to prevent a loop: {redirect}',
1216+
['redirect' => print_r($testRedirectConfig, true)]
1217+
),
1218+
__METHOD__
1219+
);
1220+
// Delete the redirect that has a redirectSrcUrl the same as this record's redirectDestUrl
1221+
try {
1222+
$db->createCommand()->delete(
1223+
'{{%retour_static_redirects}}',
1224+
['id' => $testRedirectConfig['id']]
1225+
)->execute();
1226+
} catch (Exception $e) {
1227+
Craft::error($e->getMessage(), __METHOD__);
1228+
}
12261229
}
12271230
}
12281231
// Trigger a 'afterSaveRedirect' event

0 commit comments

Comments
 (0)