Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #20309 - URL Rewrites redirect loop #26902

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions app/code/Magento/UrlRewrite/Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\UrlRewrite\Controller;

use Magento\Framework\App\Action\Forward;
use Magento\Framework\App\Action\Redirect;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http as HttpResponse;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\RouterInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
Expand All @@ -21,10 +28,10 @@
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Router implements \Magento\Framework\App\RouterInterface
class Router implements RouterInterface
{
/**
* @var \Magento\Framework\App\ActionFactory
* @var ActionFactory
*/
protected $actionFactory;

Expand All @@ -34,7 +41,7 @@ class Router implements \Magento\Framework\App\RouterInterface
protected $url;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $storeManager;

Expand All @@ -49,17 +56,17 @@ class Router implements \Magento\Framework\App\RouterInterface
protected $urlFinder;

/**
* @param \Magento\Framework\App\ActionFactory $actionFactory
* @param ActionFactory $actionFactory
* @param UrlInterface $url
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResponseInterface $response
* @param StoreManagerInterface $storeManager
* @param ResponseInterface $response
* @param UrlFinderInterface $urlFinder
*/
public function __construct(
\Magento\Framework\App\ActionFactory $actionFactory,
ActionFactory $actionFactory,
UrlInterface $url,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResponseInterface $response,
StoreManagerInterface $storeManager,
ResponseInterface $response,
UrlFinderInterface $urlFinder
) {
$this->actionFactory = $actionFactory;
Expand All @@ -83,24 +90,25 @@ public function match(RequestInterface $request)
$this->storeManager->getStore()->getId()
);

if ($rewrite === null) {
//No rewrite rule matching current URl found, continuing with
//processing of this URL.
if ($rewrite === null || $rewrite->getRequestPath() === $rewrite->getTargetPath()) {
// Either no rewrite rule matching current URl found or found one with request path equal to
// target path, continuing with processing of this URL.
return null;
}

if ($rewrite->getRedirectType()) {
//Rule requires the request to be redirected to another URL
//and cannot be processed further.
// Rule requires the request to be redirected to another URL
// and cannot be processed further.
return $this->processRedirect($request, $rewrite);
}
//Rule provides actual URL that can be processed by a controller.
// Rule provides actual URL that can be processed by a controller.
$request->setAlias(
UrlInterface::REWRITE_REQUEST_PATH_ALIAS,
$rewrite->getRequestPath()
);
$request->setPathInfo('/' . $rewrite->getTargetPath());
return $this->actionFactory->create(
\Magento\Framework\App\Action\Forward::class
Forward::class
);
}

Expand Down
13 changes: 9 additions & 4 deletions app/code/Magento/UrlRewrite/Model/Storage/DbStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,22 @@ private function extractMostRelevantUrlRewrite(string $requestPath, array $urlRe
{
$prioritizedUrlRewrites = [];
foreach ($urlRewrites as $urlRewrite) {
$urlRewriteRequestPath = $urlRewrite[UrlRewrite::REQUEST_PATH];
$urlRewriteTargetPath = $urlRewrite[UrlRewrite::TARGET_PATH];
switch (true) {
case $urlRewrite[UrlRewrite::REQUEST_PATH] === $requestPath:
case rtrim($urlRewriteRequestPath, '/') === rtrim($urlRewriteTargetPath, '/'):
$priority = 99;
break;
case $urlRewriteRequestPath === $requestPath:
$priority = 1;
break;
case $urlRewrite[UrlRewrite::REQUEST_PATH] === urldecode($requestPath):
case $urlRewriteRequestPath === urldecode($requestPath):
$priority = 2;
break;
case rtrim($urlRewrite[UrlRewrite::REQUEST_PATH], '/') === rtrim($requestPath, '/'):
case rtrim($urlRewriteRequestPath, '/') === rtrim($requestPath, '/'):
$priority = 3;
break;
case rtrim($urlRewrite[UrlRewrite::REQUEST_PATH], '/') === rtrim(urldecode($requestPath), '/'):
case rtrim($urlRewriteRequestPath, '/') === rtrim(urldecode($requestPath), '/'):
$priority = 4;
break;
default:
Expand Down
Loading