2

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.

  • Your htaccess parameters will always be overwritten by the PHP code. Did you try doing a var_dump on $_SERVER['HTTP_HOST'] to see what it returns? Also are the $mageRunCode and $mageRunType variables defined before the switch? You may end up with empty variables that will always replace the htaccess values, therefor pointing you to the default storeview – Alex Dinca May 9 '17 at 15:12
  • var_dum on $_SERVER['HTTP_HOST'] return dev.xyz.com and $mageRunCode and $mageRunType variables are not defined before switch, they return NULL before switch. – Waseem Abbas May 11 '17 at 7:09
6

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.
  • I can confirm the above solution also works for Magento 2.1.10 Commerce Edition. – Asrar Sep 19 '18 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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