From d6adc55400f5cfbcc04fc327557811a19d21f390 Mon Sep 17 00:00:00 2001 From: Nils Preuss Date: Tue, 11 Jun 2019 09:55:50 +0200 Subject: [PATCH 1/7] Add RewriteController to allow easy handling of historic rewrites --- .../controllers/RewriteController.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php new file mode 100644 index 0000000..2d00de2 --- /dev/null +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -0,0 +1,27 @@ +_checkHttpMethod('GET')) { + return $this->_result(500, 'Only GET method allowed'); + } + $requestPath = $this->getRequest()->getParam('request_path'); + if ($requestPath) { + + $reader = Mage::getSingleton('core/resource')->getConnection('core_read'); + $select = $reader->select() + ->from('core_url_rewrite', ['target_path'])->where('request_path = ?', $requestPath); + $result = $reader->fetchOne($select); + + if ($result) { + return $this->_result(200, $result); + } + } + + return $this->_result(500, 'Not possible to retrieve target location'); + } + +} From 50c5af8ce17acd6573d67a1abaa6ab4043c37a39 Mon Sep 17 00:00:00 2001 From: Nils Preuss Date: Wed, 3 Jul 2019 11:08:15 +0200 Subject: [PATCH 2/7] Add LIMIT clause to normalize performance for big redirect databases Though using fetchOne() returns the expcted result it is internally limiting the result after querying it in full so this could lead to performance impact on bigger cataloges to prevent this we added the limitation directly to the sql query. --- .../VueStorefrontBridge/controllers/RewriteController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php index 2d00de2..1f8705f 100644 --- a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -13,7 +13,9 @@ public function targetAction() $reader = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $reader->select() - ->from('core_url_rewrite', ['target_path'])->where('request_path = ?', $requestPath); + ->from('core_url_rewrite', ['target_path']) + ->where('request_path = ?', $requestPath) + ->limit(1); $result = $reader->fetchOne($select); if ($result) { From 3e3b407a71d7da3d93e0c5d989a9c2acb3f759f9 Mon Sep 17 00:00:00 2001 From: Nils Preuss Date: Wed, 3 Jul 2019 14:18:57 +0200 Subject: [PATCH 3/7] Added store_id as parameter As core_url_rewrite stores rewrites based on store_id and request_path we need both to identify a correct target. Added lookup of default store_id=0 to the request to handle unspecific redirects at all. Order by is needed to ensure, if default and store specific rewrites are found, the default one is skipped and the more specific is used. --- .../VueStorefrontBridge/controllers/RewriteController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php index 1f8705f..20f3b25 100644 --- a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -9,12 +9,15 @@ public function targetAction() return $this->_result(500, 'Only GET method allowed'); } $requestPath = $this->getRequest()->getParam('request_path'); + $storeId = $this->getRequest()->getParam('store_id'); if ($requestPath) { $reader = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $reader->select() ->from('core_url_rewrite', ['target_path']) ->where('request_path = ?', $requestPath) + ->where('store_id IN (?)', ['0', $storeId]) + ->order('store_id DESC') ->limit(1); $result = $reader->fetchOne($select); From 1f2e9d2ae6f053ce37742c383734c3bf037f512a Mon Sep 17 00:00:00 2001 From: Nils Preuss Date: Wed, 3 Jul 2019 14:20:36 +0200 Subject: [PATCH 4/7] Improve status code handling Added more specific http-status codes for different types of possible results as 500 was not really the correct one to use. --- .../VueStorefrontBridge/controllers/RewriteController.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php index 20f3b25..8434c1b 100644 --- a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -6,7 +6,7 @@ public function targetAction() { if (!$this->_checkHttpMethod('GET')) { - return $this->_result(500, 'Only GET method allowed'); + return $this->_result(405, 'Method not allowed'); } $requestPath = $this->getRequest()->getParam('request_path'); $storeId = $this->getRequest()->getParam('store_id'); @@ -24,9 +24,11 @@ public function targetAction() if ($result) { return $this->_result(200, $result); } + return $this->_result(404, 'No matching request path found'); } - return $this->_result(500, 'Not possible to retrieve target location'); + return $this->_result(400, 'Malforemd request'); + } } From 539a48686c8a5b98198de8925608120ec1755a01 Mon Sep 17 00:00:00 2001 From: ResuBaka Date: Wed, 3 Jul 2019 15:32:55 +0200 Subject: [PATCH 5/7] Update RewriteController.php Improved store_id values in sql where --- .../controllers/RewriteController.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php index 8434c1b..623e5b0 100644 --- a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -1,34 +1,35 @@ _checkHttpMethod('GET')) { return $this->_result(405, 'Method not allowed'); } + $requestPath = $this->getRequest()->getParam('request_path'); - $storeId = $this->getRequest()->getParam('store_id'); if ($requestPath) { $reader = Mage::getSingleton('core/resource')->getConnection('core_read'); + $select = $reader->select() ->from('core_url_rewrite', ['target_path']) ->where('request_path = ?', $requestPath) - ->where('store_id IN (?)', ['0', $storeId]) + ->where('store_id IN (?)', [Mage_Core_Model_App::ADMIN_STORE_ID, (int)Mage::app()->getStore()->getId()]) ->order('store_id DESC') ->limit(1); + $result = $reader->fetchOne($select); if ($result) { return $this->_result(200, $result); } + return $this->_result(404, 'No matching request path found'); } return $this->_result(400, 'Malforemd request'); } - } From 92e6e460d720704080fdba7001ef18e966f49590 Mon Sep 17 00:00:00 2001 From: ResuBaka Date: Wed, 3 Jul 2019 15:39:57 +0200 Subject: [PATCH 6/7] Update RewriteController.php Formatting --- .../VueStorefrontBridge/controllers/RewriteController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php index 623e5b0..d933db4 100644 --- a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -10,7 +10,6 @@ public function targetAction() $requestPath = $this->getRequest()->getParam('request_path'); if ($requestPath) { - $reader = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $reader->select() From 2acfdfd12d83d7c825d6f8f054474a86a40768be Mon Sep 17 00:00:00 2001 From: ResuBaka Date: Fri, 5 Jul 2019 12:34:59 +0200 Subject: [PATCH 7/7] Check that rewrite is not internal product or category rewrite --- .../VueStorefrontBridge/controllers/RewriteController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php index d933db4..848242f 100644 --- a/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php +++ b/magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/RewriteController.php @@ -16,6 +16,8 @@ public function targetAction() ->from('core_url_rewrite', ['target_path']) ->where('request_path = ?', $requestPath) ->where('store_id IN (?)', [Mage_Core_Model_App::ADMIN_STORE_ID, (int)Mage::app()->getStore()->getId()]) + ->where('category_id is null') + ->where('product_id is null') ->order('store_id DESC') ->limit(1);