3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \UrlRewrite \Controller ;
7
9
10
+ use Magento \Framework \App \Action \Forward ;
8
11
use Magento \Framework \App \Action \Redirect ;
12
+ use Magento \Framework \App \ActionFactory ;
9
13
use Magento \Framework \App \ActionInterface ;
10
14
use Magento \Framework \App \Request \Http as HttpRequest ;
11
15
use Magento \Framework \App \RequestInterface ;
12
16
use Magento \Framework \App \Response \Http as HttpResponse ;
17
+ use Magento \Framework \App \ResponseInterface ;
18
+ use Magento \Framework \App \RouterInterface ;
13
19
use Magento \Framework \Exception \NoSuchEntityException ;
14
20
use Magento \Framework \UrlInterface ;
21
+ use Magento \Store \Model \StoreManagerInterface ;
15
22
use Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite ;
16
23
use Magento \UrlRewrite \Model \UrlFinderInterface ;
17
24
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
21
28
*
22
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
30
*/
24
- class Router implements \ Magento \ Framework \ App \ RouterInterface
31
+ class Router implements RouterInterface
25
32
{
26
33
/**
27
- * @var \Magento\Framework\App\ ActionFactory
34
+ * @var ActionFactory
28
35
*/
29
36
protected $ actionFactory ;
30
37
@@ -34,7 +41,7 @@ class Router implements \Magento\Framework\App\RouterInterface
34
41
protected $ url ;
35
42
36
43
/**
37
- * @var \Magento\Store\Model\ StoreManagerInterface
44
+ * @var StoreManagerInterface
38
45
*/
39
46
protected $ storeManager ;
40
47
@@ -49,17 +56,17 @@ class Router implements \Magento\Framework\App\RouterInterface
49
56
protected $ urlFinder ;
50
57
51
58
/**
52
- * @param \Magento\Framework\App\ ActionFactory $actionFactory
59
+ * @param ActionFactory $actionFactory
53
60
* @param UrlInterface $url
54
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
55
- * @param \Magento\Framework\App\ ResponseInterface $response
61
+ * @param StoreManagerInterface $storeManager
62
+ * @param ResponseInterface $response
56
63
* @param UrlFinderInterface $urlFinder
57
64
*/
58
65
public function __construct (
59
- \ Magento \ Framework \ App \ ActionFactory $ actionFactory ,
66
+ ActionFactory $ actionFactory ,
60
67
UrlInterface $ url ,
61
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
62
- \ Magento \ Framework \ App \ ResponseInterface $ response ,
68
+ StoreManagerInterface $ storeManager ,
69
+ ResponseInterface $ response ,
63
70
UrlFinderInterface $ urlFinder
64
71
) {
65
72
$ this ->actionFactory = $ actionFactory ;
@@ -84,23 +91,41 @@ public function match(RequestInterface $request)
84
91
);
85
92
86
93
if ($ rewrite === null ) {
87
- //No rewrite rule matching current URl found, continuing with
88
- //processing of this URL.
94
+ // No rewrite rule matching current URl found, continuing with
95
+ // processing of this URL.
89
96
return null ;
90
97
}
98
+
99
+ $ requestStringTrimmed = ltrim ($ request ->getRequestString (), '/ ' );
100
+ $ rewriteRequestPath = $ rewrite ->getRequestPath ();
101
+ $ rewriteTargetPath = $ rewrite ->getTargetPath ();
102
+ $ rewriteTargetPathTrimmed = ltrim ($ rewriteTargetPath , '/ ' );
103
+
104
+ if (preg_replace ('/\?.*/ ' , '' , $ rewriteRequestPath ) === preg_replace ('/\?.*/ ' , '' , $ rewriteTargetPath ) &&
105
+ (
106
+ !$ requestStringTrimmed ||
107
+ !$ rewriteTargetPathTrimmed ||
108
+ strpos ($ requestStringTrimmed , $ rewriteTargetPathTrimmed ) === 0
109
+ )
110
+ ) {
111
+ // Request and target paths of rewrite found without query params are equal and current request string
112
+ // starts with request target path, continuing with processing of this URL.
113
+ return null ;
114
+ }
115
+
91
116
if ($ rewrite ->getRedirectType ()) {
92
- //Rule requires the request to be redirected to another URL
93
- //and cannot be processed further.
117
+ // Rule requires the request to be redirected to another URL
118
+ // and cannot be processed further.
94
119
return $ this ->processRedirect ($ request , $ rewrite );
95
120
}
96
- //Rule provides actual URL that can be processed by a controller.
121
+ // Rule provides actual URL that can be processed by a controller.
97
122
$ request ->setAlias (
98
123
UrlInterface::REWRITE_REQUEST_PATH_ALIAS ,
99
- $ rewrite -> getRequestPath ()
124
+ $ rewriteRequestPath
100
125
);
101
- $ request ->setPathInfo ('/ ' . $ rewrite -> getTargetPath () );
126
+ $ request ->setPathInfo ('/ ' . $ rewriteTargetPath );
102
127
return $ this ->actionFactory ->create (
103
- \ Magento \ Framework \ App \ Action \ Forward::class
128
+ Forward::class
104
129
);
105
130
}
106
131
0 commit comments