Magento – Magento 2 with multiple domain names

magento2multidomain

I have Magento 2 with two store views: one with code "a" and one with code "b".

I have set up two domains mydomain.a and mydomain.b,  with public folders

/a/public_html/

/b/public_html/

now

/a/public_html/index.php looks like

<?php
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'a';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

and /b/public_html/index.php looks the same except that I have 'b' in the 3rd line.

in /b/public_html/ I have set up symbolic links to all folders in /a/public_html/ (app, bin, dev, pub,…).

the "base URL" properties for store view "a" is "http://www.mydomain.a" and for "b" "http://www.mydomain.b".

But when I go to mysite.a it looks fine, but when I go to mysite.b it redirects to mysite.a and language is a, not b.
And when I go to either mysite.b/admin or mysite.a/admin it starts to loop from a to b and I get "This website has a redirect loop".

Isn't this super weird? Perhaps if I have two full installations of Meganto 2 and just a common database, would this work?

Best Answer

Maybe this will help anybody, I use this in my index.php and works like a charm :)

switch ($_SERVER['HTTP_HOST']) {
    case 'www.domain-x.com':
    case 'domain-x.com':
        $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'x'; 
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; 
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); 
        $app = $bootstrap->createApplication('Magento\Framework\App\Http'); 
        $bootstrap->run($app); 
        break; 
    case 'www.domain-y.com':
    case 'domain-y.com':
        $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'y'; 
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; 
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); 
        $app = $bootstrap->createApplication('Magento\Framework\App\Http'); 
        $bootstrap->run($app); 
    break; 

    default: 
        Mage::run(); 
    break; 
}
Related Topic