Skip to content

Commit 81dece4

Browse files
committed
Changed defaultStoreCode lookup in URL
1 parent 9deab4e commit 81dece4

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

Observer/RedirectWithoutStoreCode.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,36 @@ public function __construct(
4949
*/
5050
public function execute(\Magento\Framework\Event\Observer $observer)
5151
{
52-
$websiteId = $this->storeManager->getStore()->getWebsiteId();
53-
$defaultStore = $this->storeManager->getWebsite($websiteId)->getDefaultStore();
52+
if ($this->config->isHideDefaultStoreCode() && $code = $this->config->getRedirectCode()) {
53+
$websiteId = $this->storeManager->getStore()->getWebsiteId();
54+
$defaultStore = $this->storeManager->getWebsite($websiteId)->getDefaultStore();
5455

55-
if (!is_null($defaultStore)) {
56-
$url = $this->url->getCurrentUrl();
57-
$pos = strpos($url, $this->storeManager->getStore()->getBaseUrl() . $defaultStore->getCode());
56+
if (!is_null($defaultStore)) {
57+
$url = $this->url->getCurrentUrl();
58+
if ($this->isUrlWithDefaultCode($url, $defaultStore->getCode())) {
59+
$controller = $observer->getData('controller_action');
60+
$url = str_replace('/' . $defaultStore->getCode() . '/', '/', $url);
61+
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
62+
$controller->getResponse()->setRedirect($url, $code);
63+
}
64+
}
65+
}
66+
}
5867

59-
if (
60-
$this->config->isHideDefaultStoreCode() &&
61-
$pos !== false &&
62-
$code = $this->config->getRedirectCode()
63-
) {
64-
$controller = $observer->getData('controller_action');
65-
$url = str_replace('/' . $defaultStore->getCode() . '/', '/', $url);
66-
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
67-
$controller->getResponse()->setRedirect($url, $code);
68+
/**
69+
* @param string $url
70+
* @param string $defaultCode
71+
* @return bool
72+
*/
73+
private function isUrlWithDefaultCode($url, $defaultCode)
74+
{
75+
$urlParts = parse_url($url);
76+
if (isset($urlParts['path'])) {
77+
$pathParts = explode('/', ltrim($urlParts['path'], '/'), 2);
78+
if (isset($pathParts[0]) && $pathParts[0] == $defaultCode) {
79+
return true;
6880
}
6981
}
82+
return false;
7083
}
7184
}

0 commit comments

Comments
 (0)