Magento – Magento 2 Multistore is not working with multi domain

.htaccessmagento2multistore

I am facing issue while creating multi store in magento 2. I have added 2 store abc and xyz, each points to different domain.

I have added suggested code from different sites.

In index.php I added following code

switch($_SERVER['HTTP_HOST']) {
 case 'dev.abc.com':
        $mageRunCode='abc';
        $mageRunType='store';
        break;
  case 'dev.xyz.com':
        $mageRunCode='xyz';
        $mageRunType='store';
        break;
}
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $mageRunCode;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = $mageRunType;
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

In .htaccess file I added

SetEnvIf Host dev.abc.com MAGE_RUN_CODE=abc
SetEnvIf Host dev.abc.com MAGE_RUN_TYPE=store

SetEnvIf Host dev.xyz.com MAGE_RUN_CODE=xyz
SetEnvIf Host dev.xyz.com MAGE_RUN_TYPE=store

Base url is also set for both store correctly in store configuration.

Now the problem is xyz store with dev.xyz.com is working fine however when i switch to abc store using store switcher its always redirect to xyz store dev.xyz.com

Please suggest me any solution. Is this store switcher issue or issue with multi domain, and how I will fix this issue.

Best Answer

I had the very same problem with MAGENTO 2.1.7 . After reading thousands (literally) of blogs, tutorials and websites I figured it out by sticking a few lines of code.

1 : Do not touch the .HTACCESS file. There's nothing to modify in there.

2 : Open INDEX.PHP and replace the following code

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

by this code :

$params = $_SERVER;

switch($_SERVER['HTTP_HOST']) {

        case 'Domain1.com':
        case 'www.Domain1.com':
             $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'Website_code1';
                         $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
        break;


        case 'Domain2.com':
        case 'www.Domain2.com':
             $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'Website_code2';
                         $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
        break;
    }

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

There's 4 things to replace in the above code.

  1. domain1.com and www.domain1.com = Your domain name for the first store (Should be the BASE store).
  2. Website_code1 = Website code for the first store.
  3. domain2.com and www.domain2.com = Your domain name for the second store
  4. Website_code2 = Website code for the second store.
Related Topic