Magento 2 Multi-site setup for different Domains

Magento 2 Multi-site setup for different Domains

This guide explains how to configure your Magento 2 multi-site to work on the Nimbus Hosting platform. Please note these steps have been provided to us by a number of our clients who are using Magento 2 multi-sites on the platofrm.

Before getting started make sure you have set up your websites, stores, and store views in your sites admin dashboard. For more information on how to do this please visit the link below:

https://devdocs.magento.com/guides/v2.3/config-guide/multi-site/ms%5Fwebsites.html

You will also need to make sure you have added the additional domains to the platform under your sites dashboard > Additional Domains

Steps

In order for your different stores views to display correctly on the Nimbus Hosting platform you will need to add some additional code to your sites index.php file.

For added security the platform sets the default document root for Magento 2 sites to /pub. Therefore you may need edit the index.php file inside the /pub folder.

Once you have opened your index.php file and look for the following:

$params = $_SERVER;

The multi-site code should be added after this line.

The below example shows how to add 2 additional sites to your Magento 2 site. In this case "secondomain.com" & "thirddomain.com".

You will need to add the code snippet as listed below. Change the store_code_two and store_code_three lines to match the store code that you have set up in your Magento Store Settings.

switch($_SERVER['HTTP_HOST'])  {
case 'seconddomain.com':
$mageRunCode = 'store_code_two'; // found in Stores / All Stores
$mageRunType = 'website'; // store or website
break;

case 'thirddomain.com':
$mageRunCode = 'store_code_three';
$mageRunType = 'website';
break;

default:
$mageRunCode = 'default';
$mageRunType = 'website';
break;
}

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $mageRunCode;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = $mageRunType;

To add more sites to the config, simply mirror the existing lines between "case" and "break" adding your new domains and store codes as required:

case 'fourthdomain.com':
$mageRunCode = 'store_code_four'; // found in Stores / All Stores
$mageRunType = 'website'; // store or website
break;

Magento 2.4 Steps

The above code snippet has needs some slight adjustments since the release of Magento 2.4. If you are using Magento 2.4 and higher then you'll need to follow the same process but adding the below instead:

$params = $_SERVER;
$domainName = $_SERVER['SERVER_NAME'];
switch ($domainName) {

case 'seconddomain.com':
$runCode = 'store_code_two'; // found in Stores / All Stores
$runType = 'store'; // store or website
break;

case 'thirddomain.com':
$runCode = 'store_code_three';
$runType = 'store';
break;

default:
$runType = 'website';
$runCode = 'base';
}
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $runCode;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = $runType;{