55 */
66namespace Magento \UrlRewrite \Controller ;
77
8+ use Magento \Framework \App \RequestInterface ;
89use Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite ;
910use Magento \UrlRewrite \Model \OptionProvider ;
1011use Magento \UrlRewrite \Model \UrlFinderInterface ;
1112use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
13+ use Magento \Framework \App \Request \Http as HttpRequest ;
14+ use Magento \Framework \App \Response \Http as HttpResponse ;
15+ use Magento \Framework \UrlInterface ;
16+ use Magento \Framework \App \Action \Redirect ;
17+ use Magento \Framework \App \ActionInterface ;
1218
1319/**
1420 * UrlRewrite Controller Router
@@ -23,7 +29,7 @@ class Router implements \Magento\Framework\App\RouterInterface
2329 protected $ actionFactory ;
2430
2531 /**
26- * @var \Magento\Framework\ UrlInterface
32+ * @var UrlInterface
2733 */
2834 protected $ url ;
2935
@@ -33,7 +39,7 @@ class Router implements \Magento\Framework\App\RouterInterface
3339 protected $ storeManager ;
3440
3541 /**
36- * @var \Magento\Framework\App\ResponseInterface
42+ * @var HttpResponse
3743 */
3844 protected $ response ;
3945
@@ -44,14 +50,14 @@ class Router implements \Magento\Framework\App\RouterInterface
4450
4551 /**
4652 * @param \Magento\Framework\App\ActionFactory $actionFactory
47- * @param \Magento\Framework\ UrlInterface $url
53+ * @param UrlInterface $url
4854 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
4955 * @param \Magento\Framework\App\ResponseInterface $response
5056 * @param UrlFinderInterface $urlFinder
5157 */
5258 public function __construct (
5359 \Magento \Framework \App \ActionFactory $ actionFactory ,
54- \ Magento \ Framework \ UrlInterface $ url ,
60+ UrlInterface $ url ,
5561 \Magento \Store \Model \StoreManagerInterface $ storeManager ,
5662 \Magento \Framework \App \ResponseInterface $ response ,
5763 UrlFinderInterface $ urlFinder
@@ -64,48 +70,83 @@ public function __construct(
6470 }
6571
6672 /**
67- * Match corresponding URL Rewrite and modify request
73+ * Match corresponding URL Rewrite and modify request.
6874 *
69- * @param \Magento\Framework\App\RequestInterface $request
70- * @return \Magento\Framework\App\ActionInterface|null
75+ * @param RequestInterface|HttpRequest $request
76+ *
77+ * @return ActionInterface|null
7178 */
72- public function match (\ Magento \ Framework \ App \ RequestInterface $ request )
79+ public function match (RequestInterface $ request )
7380 {
7481 if ($ fromStore = $ request ->getParam ('___from_store ' )) {
82+ //If we're in the process of switching stores then matching rewrite
83+ //rule from previous store because the URL was not changed yet from
84+ //old store's format.
7585 $ oldStoreId = $ this ->storeManager ->getStore ($ fromStore )->getId ();
76- $ oldRewrite = $ this ->getRewrite ($ request ->getPathInfo (), $ oldStoreId );
77- if ($ oldRewrite ) {
78- $ rewrite = $ this ->urlFinder ->findOneByData (
86+ $ oldRewrite = $ this ->getRewrite (
87+ $ request ->getPathInfo (),
88+ $ oldStoreId
89+ );
90+ if ($ oldRewrite && $ oldRewrite ->getRedirectType () === 0 ) {
91+ //If there is a match and it's a correct URL then just
92+ //redirecting to current store's URL equivalent,
93+ //otherwise just continuing finding a rule within current store.
94+ $ currentRewrite = $ this ->urlFinder ->findOneByData (
7995 [
8096 UrlRewrite::ENTITY_TYPE => $ oldRewrite ->getEntityType (),
8197 UrlRewrite::ENTITY_ID => $ oldRewrite ->getEntityId (),
82- UrlRewrite::STORE_ID => $ this ->storeManager ->getStore ()->getId (),
83- UrlRewrite::IS_AUTOGENERATED => 1 ,
98+ UrlRewrite::STORE_ID =>
99+ $ this ->storeManager ->getStore ()->getId (),
100+ UrlRewrite::REDIRECT_TYPE => 0 ,
84101 ]
85102 );
86- if ($ rewrite && $ rewrite ->getRequestPath () !== $ oldRewrite ->getRequestPath ()) {
87- return $ this ->redirect ($ request , $ rewrite ->getRequestPath (), OptionProvider::TEMPORARY );
103+ if ($ currentRewrite
104+ && $ currentRewrite ->getRequestPath ()
105+ !== $ oldRewrite ->getRequestPath ()
106+ ) {
107+ return $ this ->redirect (
108+ $ request ,
109+ $ this ->url ->getUrl (
110+ '' ,
111+ ['_direct ' => $ currentRewrite ->getRequestPath ()]
112+ ),
113+ OptionProvider::TEMPORARY
114+ );
88115 }
89116 }
90117 }
91- $ rewrite = $ this ->getRewrite ($ request ->getPathInfo (), $ this ->storeManager ->getStore ()->getId ());
118+
119+ $ rewrite = $ this ->getRewrite (
120+ $ request ->getPathInfo (),
121+ $ this ->storeManager ->getStore ()->getId ()
122+ );
123+
92124 if ($ rewrite === null ) {
125+ //No rewrite rule matching current URl found, continuing with
126+ //processing of this URL.
93127 return null ;
94128 }
95-
96129 if ($ rewrite ->getRedirectType ()) {
130+ //Rule requires the request to be redirected to another URL
131+ //and cannot be processed further.
97132 return $ this ->processRedirect ($ request , $ rewrite );
98133 }
99-
100- $ request ->setAlias (\Magento \Framework \UrlInterface::REWRITE_REQUEST_PATH_ALIAS , $ rewrite ->getRequestPath ());
134+ //Rule provides actual URL that can be processed by a controller.
135+ $ request ->setAlias (
136+ UrlInterface::REWRITE_REQUEST_PATH_ALIAS ,
137+ $ rewrite ->getRequestPath ()
138+ );
101139 $ request ->setPathInfo ('/ ' . $ rewrite ->getTargetPath ());
102- return $ this ->actionFactory ->create (\Magento \Framework \App \Action \Forward::class);
140+ return $ this ->actionFactory ->create (
141+ \Magento \Framework \App \Action \Forward::class
142+ );
103143 }
104144
105145 /**
106- * @param \Magento\Framework\App\ RequestInterface $request
146+ * @param RequestInterface $request
107147 * @param UrlRewrite $rewrite
108- * @return \Magento\Framework\App\ActionInterface|null
148+ *
149+ * @return ActionInterface|null
109150 */
110151 protected function processRedirect ($ request , $ rewrite )
111152 {
@@ -119,16 +160,17 @@ protected function processRedirect($request, $rewrite)
119160 }
120161
121162 /**
122- * @param \Magento\Framework\App\ RequestInterface $request
163+ * @param RequestInterface|HttpRequest $request
123164 * @param string $url
124165 * @param int $code
125- * @return \Magento\Framework\App\ ActionInterface
166+ * @return ActionInterface
126167 */
127168 protected function redirect ($ request , $ url , $ code )
128169 {
129170 $ this ->response ->setRedirect ($ url , $ code );
130171 $ request ->setDispatched (true );
131- return $ this ->actionFactory ->create (\Magento \Framework \App \Action \Redirect::class);
172+
173+ return $ this ->actionFactory ->create (Redirect::class);
132174 }
133175
134176 /**
0 commit comments