Magento 2 – Multiple Stores/Websites running different Domains

One of the distinguished features of Magento always has been the capability to run multiple Stores/Website from a single installation/back-end. This allows for simple administration and one set of master data when it comes to products and customers, but yet have totally customised frontends and user experience including using different domains. As with Magento 1.x  the values for the variables MAGE_RUN_TYPE and MAGE_RUN_CODE determine what store/website to display. A good starting point is the official developer documentation (2.0) on this topic. However, it comes short of how to explain the enable the Multistore functionality using different domain names and how to use those above variables exactly when you have more the 2 different domains. The following outlines the changes that need to be made in the entry point script index.php. No other files need to be touched or changed, however, depending on you hosting platform and setup additional modifications might be necessary particular for the rewrites of the URLs to work properly.

A prerequsit of course the the proper setup of the respective stores/websites via Admin -> Stores -> All Stores and the configuration of the receptive URLs via Admin -> Stores -> Configuration -> General -> Web -> Base URLSs (and Base URLs Secure). Once this is done and verfied, modifiy the code for the index.php as required and all domains you divert to their respective stores/websites.

/**
 * Application entry point
 *
 * Example - run a particular store or website:
 * --------------------------------------------
 * require __DIR__ . '/app/bootstrap.php';
 * $params = $_SERVER;
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
 * $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
 * \/** @var \Magento\Framework\App\Http $app *\/
 * $app = $bootstrap->createApplication('Magento\Framework\App\Http');
 * $bootstrap->run($app);
 * --------------------------------------------
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
 
try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}
 
$params = $_SERVER;
switch($_SERVER['HTTP_HOST']) {
 
		case 'doamin1.ie':
		case 'www.doamin1.ie':
			 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'ba_ie';
                         $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
		break;
 
 
		case 'doamin2.co.uk':
		case 'www.doamin2.co.uk':
			 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'ca_uk';
                         $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
		break;
 
 
		case 'doamin3.co.uk':
		case 'www.doamin3.co.uk':
			 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'ba_uk';
                         $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);

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *