Magento2: Store switcher redirects to homepage for multistore setup with different domains

Created on 14 Jan 2020  路  11Comments  路  Source: magento/magento2

Preconditions (*)

  1. 2.3.3
  2. More than one store view
  3. Separate domain for the store views (default -> mage.local, second store view -> test.mage.local)
    Apache vhost:
<VirtualHost *:80>
   ServerName          mage.local
   DocumentRoot        /var/www/html/magento2/pub/
</VirtualHost>

<VirtualHost *:80>
   ServerName          test.mage.local
   DocumentRoot        /var/www/html/magento2/pub/
   SetEnv MAGE_RUN_CODE "test"
   SetEnv MAGE_RUN_TYPE "store"
</VirtualHost>
  1. Setup base urls for both store views in Stores > Settings > Configuration > General > Web
  2. Create a category (it can be reproduced with cms pages too)

Steps to reproduce (*)

  1. Go to mage.local
  2. Go to category
  3. Use store switch to switch to the other store

Expected result (*)

  1. Redirect to test.mage.local/category-name.html

Actual result (*)

  1. Redirect to test.mage.local/

Additional Information

Probably related to #14674 and #19714

Frontend Url Fixed in 2.4.x Clear Description Confirmed Format is valid Ready for Work Reported on 2.3.3 Reproduced on 2.4.x

All 11 comments

Hi @DanieliMi. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [x] Summary of the issue
  • [x] Information on your environment
  • [x] Steps to reproduce
  • [x] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@DanieliMi do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [x] yes (on local, because the setup is not possible on the CI instances)
  • [ ] no

Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @engcom-Charlie
Thank you for verifying the issue. Based on the provided information internal tickets MC-30450 were created

Issue Available: @engcom-Charlie, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

The problem seem to exist in the request between "stores/store/redirect" and "stores/store/switch".

I have a Magento 2.4-dev setup with two stores (sv and default). The requests for the two mentioned above when switching from sv to default is
"http://sv.magento-dev.dev.int/index.php/stores/store/redirect/___store/default/___from_store/se/uenc/{unec}/"
and
"http://sv.magento-dev.dev.int/index.php/stores/store/switch/?___from_store=se&___store=default&uenc={uenc}".

The problem then appears in the \Magento\Store\Model\BaseUrlChecker::execute on line 42 which is called from \Magento\Store\App\FrontControllerPlugin\RequestPreprocessor::aroundDispatch on line 81. It checks if the host in the $uri array matches the host in the request. The $uri array is parsed using the parse_url method with the base url of the current store. To get the current store Magento uses \Magento\Store\Model\StoreManager::getStore which in turn uses the \Magento\Store\Model\StoreResolver::getCurrentStoreId. This method will retrieve the store from the url parameter ___store which in this case is "default" and the base url for my default store is http://magento-dev.dev.int. So the check if the request host (http://sv.magento-dev.dev.int) matches the base url of the store (http://magento-dev.dev.int) fails and you will be redirected incorrectly.

The request to "stores/store/swtich" should be to the new stores url and not the current. We've fixed this by setting current store before the last redirect in \Magento\Store\Controller\Store\Redirect::execute so the redirect request is made to the base url of that store.
It will need some more testing but I will make a pull request to Magento with this fix.

The pull request has been accepted so you can use this patch to fix the issue in your Magento 2.3.3 instance: https://patch-diff.githubusercontent.com/raw/magento/magento2/pull/26548.patch

The patch solving a similar issue is available in Magento Quality Patches package (MQP)

Patch
MDVA-30107: Fixes the issue where store switcher doesn't work as expected if different base URLs are used for store views.

Compatible versions
Magento OpenSource/Commerce/Commerce Cloud 2.3.0 - 2.3.5

Applying a patch - Magento OpenSource/Commerce

  1. $ composer require magento/quality-patches
  2. $ ./vendor/bin/magento-patches apply MDVA-30107

See MQP Magento Commerce documentation

Applying a patch - Magento Commerce Cloud
See MQP Magento Commerce Cloud documentation

Patch Details
https://support.magento.com/hc/en-us/articles/360049869051

Not working on Magento CE 2.3.3

Applied above patch, Preconditions, 2 store views, 1 website and 1 store.

One CMS page in both store view with same URL key, Switching store from CMS Page taking to home page.
why ? @viktym @tstoyanovski-bounteous

@magento give me 2.3-develop instance

Hi @VZeroCool. Thank you for your request. I'm working on Magento instance for you.

Hi @VZeroCool, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

Was this page helpful?
0 / 5 - 0 ratings