Skip to content

Commit

Permalink
Merge pull request #103 from studioespresso/101-craft-5-regex-redirec…
Browse files Browse the repository at this point in the history
…t-to-other-url-does-not-work

Check if we have a full url to redirect to
  • Loading branch information
janhenckens authored Dec 23, 2024
2 parents f1536e8 + a254987 commit 0b76422
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: ci
on:
workflow_dispatch:
pull_request:
push:
branches:
- develop
Expand Down
18 changes: 11 additions & 7 deletions src/services/NotFoundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,19 @@ private function getMatchingRedirect(NotFoundModel $model): RedirectRecord|array
$regexRedirects = $this->getAllRegexRedirects($model);
foreach ($regexRedirects as $regexRedirect) {
$pattern = '`' . $regexRedirect->pattern . '`i';
if (preg_match(
$pattern,
$model->urlPath
) === 1) {
$parsedUrl = preg_replace($pattern, $regexRedirect->redirect, $model->fullUrl);
return ["record" => $regexRedirect, "url" => $parsedUrl];

if (str_contains($regexRedirect->redirect, 'http')) {
$url = $model->urlPath;
} else {
$url = $model->fullUrl;
}
}

if (preg_match($pattern, $model->urlPath, $matches)) {
// Replace placeholders ($1, $2, etc.) with actual captured groups
$finalRedirectUrl = preg_replace($pattern, $regexRedirect->redirect, $url);
return ["record" => $regexRedirect, "url" => $finalRedirectUrl];
}
}
return false;
}

Expand Down

0 comments on commit 0b76422

Please sign in to comment.