From e15b7228d7a498aef7cdc3cf5f483f940d166a10 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 17 Dec 2024 12:06:39 -0500 Subject: [PATCH] refactor: If an incoming URL has a trailing slash, preserve it for things like the Canonical URL ([#1547](https://github.com/nystudio107/craft-seomatic/issues/1547)) --- src/helpers/UrlHelper.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/helpers/UrlHelper.php b/src/helpers/UrlHelper.php index 9baabb3c8..926cd4ede 100644 --- a/src/helpers/UrlHelper.php +++ b/src/helpers/UrlHelper.php @@ -150,13 +150,17 @@ public static function absoluteUrlWithProtocol($url): string } // Ensure that any spaces in the URL are encoded $url = str_replace(' ', '%20', $url); - + // If the incoming URL has a trailing slash, respect it by preserving it + $preserveTrailingSlash = false; + if (str_ends_with($url, '/')) { + $preserveTrailingSlash = true; + } // Handle trailing slashes properly for generated URLs $generalConfig = Craft::$app->getConfig()->getGeneral(); if ($generalConfig->addTrailingSlashesToUrls && !preg_match('/(.+\?.*)|(\.[^\/]+$)/', $url)) { $url = rtrim($url, '/') . '/'; } - if (!$generalConfig->addTrailingSlashesToUrls) { + if (!$generalConfig->addTrailingSlashesToUrls && !$preserveTrailingSlash) { $url = rtrim($url, '/'); }