PHPackages                             metmeer/magento2-multi-store-fix - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. metmeer/magento2-multi-store-fix

ActiveMagento2-module[Utility &amp; Helpers](/categories/utility)

metmeer/magento2-multi-store-fix
================================

Fix redirect loops when using both environment store definition and locale switcher.

1.0.0(9y ago)31.6k7[2 PRs](https://github.com/METMEER/magento2-multi-store-fix/pulls)MITPHPPHP ~5.6.0|~7.0.0

Since May 20Pushed 7y ago4 watchersCompare

[ Source](https://github.com/METMEER/magento2-multi-store-fix)[ Packagist](https://packagist.org/packages/metmeer/magento2-multi-store-fix)[ RSS](/packages/metmeer-magento2-multi-store-fix/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Multi-Store Fix Module for Magento 2
====================================

[](#multi-store-fix-module-for-magento-2)

Fix redirect loops when using multiple storeviews in Magento 2 with `MAGE_RUN_CODE` environment variables combined with the locale switcher.

Description
-----------

[](#description)

Magento 2 has a built-in store switcher (language switcher / locale switcher) that visitors can use to choose their preferred language.

[![Store Switcher](./resources/store-switcher.png "Screenshot of the store switcher in Magento's Luma theme")](./resources/store-switcher.png)

If a language is chosen, the visitor will be redirected to the same page in one of the other store views (languages). The store switcher uses a cookie to remember the selected store code between requests. This makes sense when multiple store views share the same base url, because otherwise the selected store view could not be determined.

It's common for a web shop to have multiple domain names, with each hostname showing a translated version of the same content. When a visitor selects another language, he can simple be redirected to the same page on one of the other domain names: the request hostname is used to determine the selected store view. The [recommended way to set this up](http://devdocs.magento.com/guides/v2.1/config-guide/multi-site/ms_over.html) is by configuring the web server to set the environment variable `MAGE_RUN_TYPE` to `store` and `MAGE_RUN_CODE` to the correct store code depending on the requested hostname.

If a store code is found in both the store switcher cookie *and* the server environment variable, Magento uses the one **from the cookie**. This doesn't make sense. It breaks store switching and can even cause a redirect loop.

### Example

[](#example)

Imagine the following Magento 2 web shop with two store views:

Base URLStore CodeStore NamedefaultEnglishnlDutch1. Visitor opens `http://myshop.com/`. While processing the request, Magento finds no cookie, but the environment variable is set to `default` so the *English* store view is shown.
2. Visitor selects *Dutch* from the locale switcher.
3. Magento sets the cookie for `myshop.com` to `nl` and redirects the visitor to `http://myshop.nl/`.
4. Visitor's browser requests `http://myshop.nl/`. Magento finds no cookie (as it was set for the other domain), but the environment variable is set to `nl` so the *Dutch* store view is shown. So far, no problems.
5. Now, the visitor tries to switch back to *English* using the locale switcher.
6. Magento sets the cookie for `myshop.nl` to `default` and redirects the visitor to `http://myshop.com/`.
7. Visitor's browser requests `http://myshop.com/`. Magento finds the cookie (from step 3) is set to `nl` and ignores the environment variable. It loads the *Dutch* store view, but because the hostnames do not match it redirects to `http://myshop.nl/`.
8. Visitor's browser requests `http://myshop.nl/`. Magento finds the cookie (from step 6) set to `default` and ignores the environment variable. It loads the *English* store view, but because the hostnames do not match it redirects to `http://myshop.com/`.
9. Steps 7 and 8 will be repeated until the browser gives up and shows a *Too many redirects* error message:

[![Too many redirects](./resources/too-many-redirects.png "Screenshot of the Chrome webbrowser showing the ERR_TOO_MANY_REDIRECTS error message")](./resources/too-many-redirects.png)

### Solution

[](#solution)

This module uses a plugin for Magento's `StoreCookieManager::getStoreCodeFromCookie` method which is responsible for reading the store code from the locale switcher cookie. It gets called by `StoreResolver::getCurrentStoreId` which looks up the store code in the database and returns its ID.

If a store code is set by the webserver using an environment variable, the plugin will not read the cookie but return the store code as defined by the webserver instead. This way a cookie can never override the environment variable.

The plugin checks to see if `MAGE_RUN_TYPE` is set to `store` *and* `MAGE_RUN_CODE` contains a string value. This way, it will not break other setups. For instance, when `MAGE_RUN_TYPE` is set to `website`, the problem as described above does not occur and there's no need for a fix.

Installation
------------

[](#installation)

The preferred way of installing this module is through [Composer](https://getcomposer.org/). Simply add `metmeer/magento2-multi-store-fix`as a dependency, and Composer will take care of downloading and extracting the files to the correct locations:

```
composer require metmeer/magento2-multi-store-fix

```

Optionally you can download the latest version of the module [here](https://github.com/METMEER/magento2-multi-store-fix/releases) and copy the archive's contents to your project under directory `app/code/METMEER/MultiStoreFix`.

After the files are installed, the module can be enabled with this command:

```
bin/magento module:enable METMEER_MultiStoreFix

```

Each time a new module is enabled, Magento needs to upgrade the database. This is done with the following command:

```
bin/magento setup:upgrade

```

If the deployment mode is set to *production*, the dependency injection compiler needs to be run. This is not necessary for the *developer* mode. To start the compiler, use this command:

```
bin/magento setup:di:compile

```

At last, the caches should be cleared with this command:

```
bin/magento cache:flush

```

Configuration
-------------

[](#configuration)

No further configuration is required. The fix is applied automatically when needed.

Contribution
------------

[](#contribution)

Feel free to contribute to this module by reporting issues or creating pull requests for improvements.

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Copyright
---------

[](#copyright)

Copyright (c) 2017 [METMEER](https://www.metmeer.nl/) - All rights reserved.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3328d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d917b4c52af35824961635dddaf557f2a6427c97c4504508c3425773ec5365b?d=identicon)[metmeer](/maintainers/metmeer)

---

Top Contributors

[![bastiaanh](https://avatars.githubusercontent.com/u/19793231?v=4)](https://github.com/bastiaanh "bastiaanh (1 commits)")

### Embed Badge

![Health badge](/badges/metmeer-magento2-multi-store-fix/health.svg)

```
[![Health](https://phpackages.com/badges/metmeer-magento2-multi-store-fix/health.svg)](https://phpackages.com/packages/metmeer-magento2-multi-store-fix)
```

###  Alternatives

[baldwin/magento2-module-url-data-integrity-checker

Magento 2 module which can find potential url related problems in your catalog data

282806.2k](/packages/baldwin-magento2-module-url-data-integrity-checker)[semaio/magento2-configimportexport

Import/Export core\_config\_data values in Magento 2

165768.0k3](/packages/semaio-magento2-configimportexport)[run-as-root/magento2-prometheus-exporter

Magento2 Prometheus Exporter

68353.9k](/packages/run-as-root-magento2-prometheus-exporter)[firegento/magesetup2

MageSetup provides the necessary configuration (system config, tax, agreements, etc. for a national market.

121340.5k1](/packages/firegento-magesetup2)[fishpig/magento2-wordpress-integration

Securely integrate WordPress and Magento 2.

83363.7k6](/packages/fishpig-magento2-wordpress-integration)[smile/module-retailer

Smile Retailer Suite - Retailer Module

15536.9k7](/packages/smile-module-retailer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
