Skip to content

Commit

Permalink
refactor: If an incoming URL has a trailing slash, preserve it for th…
Browse files Browse the repository at this point in the history
…ings like the Canonical URL ([#1547](#1547))
  • Loading branch information
khalwat committed Dec 17, 2024
1 parent 9383db1 commit e15b722
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/helpers/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
}

Expand Down

0 comments on commit e15b722

Please sign in to comment.