3

Everytime I'm switching from the English store view to the Italian's one and viceversa, it takes me to the equivalent homepage (no matters where I am) and it throws this error:

The store that was requested wasn't found. Verify the store and try again.

Here's my setup:

  • Magento 2.3.4 (fresh installation, self hosted)
  • Nginx reverse proxy that serves Apache webserver
  • 1 website, 1 store, 2 store views
  • For each store view one different domain (English store view --> example.com, Italian store view --> example.it)
  • I added on top of main .htaccess these env:

    SetEnvIf Host ^(.*)\.example\.com MAGE_RUN_CODE=en
    SetEnvIf Host ^(.*)\.example\.com MAGE_RUN_TYPE=store
    SetEnvIf Host ^(.*)\.example\.it MAGE_RUN_CODE=it
    SetEnvIf Host ^(.*)\.example\.it MAGE_RUN_TYPE=store
    

Recap: If, for example, I'm on example.com/my-beautiful-product.html [English store view] and I'm switching to the Italian store view, it takes me to example.it and it shows that error ("The store that was requested wasn't found. Verify the store and try again.") instead of take me on example.it/my-beautiful-product.html without any errors.

Any Ideas?


What I tested:

  • I tried to remove the NGINX reverse proxy in front of it, just exposing Apache. Same result, it does not work.

  • I tried to hardcode the store view codes in /vendor/magento/module-store/Controller/Store/SwitchAction.php at row 106 and the $requestedUrlToRedirect :

    ...
    public function execute()
    {
        $targetStoreCode = $this->_request->getParam(
            \Magento\Store\Model\StoreManagerInterface::PARAM_NAME
        );
        $fromStoreCode = $this->_request->getParam(
            '___from_store',
            $this->storeCookieManager->getStoreCodeFromCookie()
        );
    
        $requestedUrlToRedirect = 'https://example.it/my-beautiful-product.html';
        $redirectUrl = $requestedUrlToRedirect;
        // $requestedUrlToRedirect = $this->_redirect->getRedirectUrl();
        // $redirectUrl = $requestedUrlToRedirect;
    
        $error = null;
        try {
            $fromStore = $this->storeRepository->get('en');
            $targetStore = $this->storeRepository->getActiveStoreByCode('it');
            // $fromStore = $this->storeRepository->get($fromStoreCode);
            // $targetStore = $this->storeRepository->getActiveStoreByCode($targetStoreCode);
        } catch (StoreIsInactiveException $e) {
            $error = __('Requested store is inactive');
        } catch (NoSuchEntityException $e) {
            $error = __("The store that was requested wasn't found. Verify the store and try again.");
        }
        if ($error !== null) {
            $this->messageManager->addErrorMessage($error);
        } else {
            $redirectUrl = $this->storeSwitcher->switch($fromStore, $targetStore, $requestedUrlToRedirect);
        }
    
        $this->getResponse()->setRedirect($redirectUrl);
    }
    ...
    

    Here a switch url example: https://example.com/stores/store/redirect/___store/it/___from_store/en/uenc/aHR0cHM6Ly9kZXYudGVjbmljbWFuLml0Lz9fX19zdG9yZT1pdA%2C%2C/

Then I switched from Italian store view to the English one and it worked! So it seems it does not able to get the correct values of $targetStoreCode, and $requestedUrlToRedirect. Any ideas?


Update

  • On Magento 2.2.11 (fresh installation), it works great out of the box.
  • On Magento 2.3.0 (fresh installation), it works partially out of the box. Url rewrite has been created just for 1 store view. So, to make it works I had to add manually the url rewrites for the other store view. Then it worked.
  • On Magento 2.3.1 (fresh installation), it shows the error. The behaviour is just the same as for the 2.3.4 version.

At this point I believe it's a bug. I reported it on the official Magento Github repository.

2
  • 1
    issue resolved or not ? Mar 19, 2020 at 12:44
  • 1
    @TirthPatel The issue is NOT resolved. Any ideas? Thanks
    – KaMZaTa
    Mar 19, 2020 at 18:21

2 Answers 2

1

It's a Magento 2.3.1 to 2.3.4 bug. The problem is in the view... and exactly in module-store/view/frontend/templates/switch/languages.phtml at the line 28.

WRONG

<li class="view-<?= $block->escapeHtml($_lang->getCode()) ?> switcher-option">
    <a href="<?= $block->escapeUrl($block->getViewModel()->getTargetStoreRedirectUrl($_lang)) ?>">
        <?= $block->escapeHtml($_lang->getName()) ?>
    </a>
</li>

CORRECT

<li class="view-<?= $block->escapeHtml($_lang->getCode()) ?> switcher-option">
    <a href="#" data-post='<?= /* @noEscape */ $block->getTargetStorePostData($_lang) ?>'>
        <?= $block->escapeHtml($_lang->getName()) ?>
    </a>
</li>

...and now it works like a charm!

0

On my configuration for a Magento 2 with several stores I am using

website

and not

store

for the type and it works.

I have 4 websites, each with a different store (and view).

So something like

SetEnvIf Host ^(.*)\.example\.com MAGE_RUN_CODE=en
SetEnvIf Host ^(.*)\.example\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^(.*)\.example\.it MAGE_RUN_CODE=it
SetEnvIf Host ^(.*)\.example\.it MAGE_RUN_TYPE=website

might work for you ?

5
  • Thanks but it does NOT work since I have just 1 website and 2 different store views.
    – KaMZaTa
    Mar 20, 2020 at 13:39
  • Yep sorry, I read your question a bit too quick. Well, if it's not a typo I have no idea, as it seems you do it right. Can you possibly add a screen copy of your website/store configuration ? Mar 20, 2020 at 14:46
  • Here's the screen: ibb.co/kg1yqNr
    – KaMZaTa
    Mar 20, 2020 at 14:58
  • ok, then it looks like it should work. Last idea : have you check your Apache logs to be sure it gets the right domain name in the referer (since there is a nginx first) ? If yes, is your magento root on / or /pub ? To test if it uses the htaccess you've changed, make a typo on purpose and see if it crashes. If it's not something like that, well sorry I have no idea left :-) Mar 20, 2020 at 16:01
  • Yes, I have. The referer in Apache logs is correct. My Magento root is on /. I really don't know where I am wrong.
    – KaMZaTa
    Mar 21, 2020 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.