Skip to content

Commit

Permalink
Fix multilang redirects and routes
Browse files Browse the repository at this point in the history
Multilang redirects and routes were not working due to language prefixes
being included in URL when trying to match source URL and redirect/route
source pattern.

Using `$route` parameter fixes the issue as given route does not include
language prefixes (coming from `$uri->path()` in `PagesServiceProvider`).

Fixes getgrav#2435.
  • Loading branch information
nbusseneau committed Sep 12, 2020
1 parent 88c0617 commit 8c1f5a7
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions system/src/Grav/Common/Page/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,20 +533,14 @@ public function dispatch($route, $all = false, $redirect = true)
if ($site_route) {
$page = $this->dispatch($site_route, $all);
} else {

/** @var Uri $uri */
$uri = $this->grav['uri'];
/** @var \Grav\Framework\Uri\Uri $source_url */
$source_url = $uri->uri(false);

// Try Regex style redirects
$site_redirects = $config->get('site.redirects');
if (is_array($site_redirects)) {
foreach ((array)$site_redirects as $pattern => $replace) {
$pattern = '#^' . str_replace('/', '\/', ltrim($pattern, '^')) . '#';
try {
$found = preg_replace($pattern, $replace, $source_url);
if ($found !== $source_url) {
$found = preg_replace($pattern, $replace, $route);
if ($found !== $route) {
$this->grav->redirectLangSafe($found);
}
} catch (ErrorException $e) {
Expand All @@ -561,8 +555,8 @@ public function dispatch($route, $all = false, $redirect = true)
foreach ((array)$site_routes as $pattern => $replace) {
$pattern = '#^' . str_replace('/', '\/', ltrim($pattern, '^')) . '#';
try {
$found = preg_replace($pattern, $replace, $source_url);
if ($found !== $source_url) {
$found = preg_replace($pattern, $replace, $route);
if ($found !== $route) {
$page = $this->dispatch($found, $all);
}
} catch (ErrorException $e) {
Expand Down

0 comments on commit 8c1f5a7

Please sign in to comment.