Skip to content

Commit

Permalink
refactor: Retour now strips site path prefixes from incoming 404 URL …
Browse files Browse the repository at this point in the history
…paths, such that redirects will work as expected (any Site sub-path prefix is ignored) ([#288](#288))
  • Loading branch information
khalwat committed Sep 12, 2024
1 parent 94f7165 commit e5aadb7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
38 changes: 35 additions & 3 deletions src/helpers/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace nystudio107\retour\helpers;

use craft\errors\SiteNotFoundException;
use craft\helpers\UrlHelper as CraftUrlHelper;

/**
Expand Down Expand Up @@ -45,15 +46,46 @@ public static function combineQueryStringsFromUrls(...$urls): string
}

/**
* Merge the $url and $path together, combining any overlapping path segments
* Strip out any site-defined sub-path from the incoming $path
* e.g. if the Site's baseUrl is set to https://example.com/es and $path is /es/blog
* this function will return /blog
*
* @param string $url
* @param string $path
* @return string
*/
public static function mergeUrlWithPath(string $url = '', string $path = ''): string
public static function stripSitePathPrefix($path): string
{
try {
$baseSiteUrl = self::baseSiteUrl();
} catch (SiteNotFoundException $e) {
$baseSiteUrl = '';
}
$sitePath = parse_url($baseSiteUrl, PHP_URL_PATH);
if (!empty($sitePath)) {
// Normalizes a URI path by trimming leading/ trailing slashes and removing double slashes
$sitePath = '/' . preg_replace('/\/\/+/', '/', trim($sitePath, '/'));
}
// Strip the $sitePath from the incoming $path
if (str_starts_with($path, $sitePath)) {
$path = substr($path, strlen($sitePath));
$path = '/' . preg_replace('/\/\/+/', '/', trim($path, '/'));
}

return $path;
}

/**
* Merge the $url and $path together, combining any overlapping path segments
*
* @param ?string $url
* @param ?string $path
* @return string
*/
public static function mergeUrlWithPath($url, $path): string
{
$overlap = 0;
$url = $url ?? '';
$path = $path ?? '';
$urlOffset = strlen($url);
$pathLength = strlen($path);
$pathOffset = 0;
Expand Down
6 changes: 4 additions & 2 deletions src/services/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,15 @@ public function handle404()
$pathOnly = '';
$fullUrl = '';
}
// Stash the $pathOnly for use when incrementing the statistics
$originalPathOnly = $pathOnly;
// Strip out any site-defined baseUrl path prefixes
$pathOnly = UrlHelper::stripSitePathPrefix($pathOnly);
// Strip the query string if `alwaysStripQueryString` is set
if (Retour::$settings->alwaysStripQueryString) {
$fullUrl = UrlHelper::stripQueryString($fullUrl);
$pathOnly = UrlHelper::stripQueryString($pathOnly);
}
// Stash the $pathOnly for use when incrementing the statistics
$originalPathOnly = $pathOnly;
Craft::info(
Craft::t(
'retour',
Expand Down

0 comments on commit e5aadb7

Please sign in to comment.