From 576227226d75b8d78ae8c265a792f403febe9f57 Mon Sep 17 00:00:00 2001 From: Lewis Voncken Date: Fri, 24 Apr 2020 13:34:39 +0200 Subject: [PATCH] [FEATURE] Force redirect from original store code to the stripped store code --- CHANGELOG.md | 81 +++++++++++++++++++ .../App/Request/StorePathInfoValidator.php | 41 ++++++++-- 2 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..759fd58 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,81 @@ +## 2.1.0 (2020-04-24) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.1.0) + +* [BUGFIX] Made the Store Plugin Global to solve issue invalid urls in the sitemap.xml *(Lewis Voncken)* +* [FEATURE] Force redirect from original store code to the stripped store code *(Lewis Voncken)* + + +## 2.0.5 (2020-03-16) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.0.5) + +* [BUGFIX] Solved invalid issue when using www in domain *(Mr. Lewis)* + + +## 2.0.4 (2020-03-04) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.0.4) + +* [BUGFIX] Modified website matching by stripping schema and www. *(Egor Dmitriev)* + + +## 2.0.3 (2020-03-02) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.0.3) + +* [BUGFIX] Fixed direct links to an aliased storeview. *(Egor Dmitriev)* + + +## 2.0.2 (2020-02-24) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.0.2) + +* [BUGFIX] Fixed fallback such that urls with code in form of website_code still work. *(Egor Dmitriev)* + + +## 2.0.1 (2019-12-18) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.0.1) + +* [BUGFIX] fixed storeswitcher functionality. Fixed issue with navigating on a non default store. Before all pages would load the default store in de back. Now the correct store *(thomas mondeel)* +* Update README.md *(thokiller)* +* [BUGFIX] fixed switching from homepage *(thomas mondeel)* + + +## 2.0.0 (2019-12-11) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/2.0.0) + +* [FEATURE/BUGFIX] made module compatible with Magento 2.3.* *(thokiller)* + + +## 1.0.3 (2018-05-11) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/1.0.3) + +* [BUGFIX] Solved fatal php error Call to a member function getCode on a boolean *(Lewis Voncken)* + + +## 1.0.2 (2017-08-24) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/1.0.2) + +* [BUGFIX] Solved error in composer .json because of wrong name *(Bart Lubbersen)* + + +## 1.0.1 (2017-08-24) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/1.0.1) + +* [TASK] Add license to composer *(Bart Lubbersen)* + + +## 1.0.0 (2017-08-24) + +[View Release](git@github.com:experius/Magento2-Module-Multiple-Website-Store-Code-Url.git/commits/tag/1.0.0) + +* [TASK] Initial commit *(Bart Lubbersen)* +* [TASK] Make configuration only available on default level because url param plugin wont know store yet [TASK] Make module independent on Core module [TASK] Add composer and readme file *(Bart Lubbersen)* + + diff --git a/Plugin/Magento/Store/App/Request/StorePathInfoValidator.php b/Plugin/Magento/Store/App/Request/StorePathInfoValidator.php index de2a8a2..4a7170a 100644 --- a/Plugin/Magento/Store/App/Request/StorePathInfoValidator.php +++ b/Plugin/Magento/Store/App/Request/StorePathInfoValidator.php @@ -2,9 +2,12 @@ namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request; +use Experius\MultipleWebsiteStoreCodeUrl\Helper\Data; use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings; +use Magento\Framework\App\Request\PathInfo; use Magento\Store\Api\StoreCookieManagerInterface; use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\App\ResponseFactory; /** * Class StorePathInfoValidator @@ -24,7 +27,7 @@ class StorePathInfoValidator private $settings; /** - * @var \Experius\MultipleWebsiteStoreCodeUrl\Helper\Data + * @var Data */ private $helper; @@ -34,29 +37,38 @@ class StorePathInfoValidator private $storeCookieManager; /** - * @var \Magento\Framework\App\Request\PathInfo + * @var PathInfo */ private $pathInfo; + /** + * @var ResponseFactory + */ + private $responseFactory; /** * StorePathInfoValidator constructor. + * * @param StoreManagerInterface $storeManager * @param Settings $settings + * @param Data $helper * @param StoreCookieManagerInterface $storeCookieManager - * @param \Magento\Framework\App\Request\PathInfo $pathInfo + * @param PathInfo $pathInfo + * @param ResponseFactory $responseFactory */ public function __construct( StoreManagerInterface $storeManager, Settings $settings, - \Experius\MultipleWebsiteStoreCodeUrl\Helper\Data $helper, + Data $helper, StoreCookieManagerInterface $storeCookieManager, - \Magento\Framework\App\Request\PathInfo $pathInfo + PathInfo $pathInfo, + ResponseFactory $responseFactory ) { $this->storeManager = $storeManager; $this->settings = $settings; $this->helper = $helper; $this->storeCookieManager = $storeCookieManager; $this->pathInfo = $pathInfo; + $this->responseFactory = $responseFactory; } /** @@ -72,7 +84,7 @@ public function afterGetValidStoreCode( $request, $pathInfo = '' ) { - if ($result != null || !$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) { + if (!$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) { return $result; } if (empty($pathInfo)) { @@ -81,6 +93,13 @@ public function afterGetValidStoreCode( $request->getBaseUrl() ); } + $pathParts = explode('/', ltrim($pathInfo, '/'), 2); + if ($result) { + if (strpos($pathParts[0], '_') === false) { + return $result; + } + + } $websiteCode = $this->storeCookieManager->getStoreCodeFromCookie(); if(!$websiteCode && $website = $this->helper->getRequestToWebsite($request)) { @@ -89,7 +108,15 @@ public function afterGetValidStoreCode( if (!$websiteCode) { return $result; } - $pathParts = explode('/', ltrim($pathInfo, '/'), 2); + + if ($result && strpos($request->getRequestUri(), "/{$websiteCode}_") !== false) { + $requestUri = str_replace("/{$websiteCode}_", "", $request->getRequestUri()); + $response = $this->responseFactory->create(); + $response->setRedirect($request->getDistroBaseUrl() . $requestUri, 301); + $response->sendResponse(); + exit; + } + $storeCode = "{$websiteCode}_{$pathParts[0]}"; try { /** @var \Magento\Store\Api\Data\StoreInterface $store */