參考來源

Magento allows you to setup multi-websites for your single installed Magento. Its configuration is really simple which I’ll explain later. It will allow you to create a separate website and the real benefits are you can handle your products, customers, product price and may more things website wise. You can use these global or website wise depends on your needs.

You must have 2 different domains to setup multi-websites. like- www.magentotest1.com & test.magentotest1.com.

The first thing you need to do is log in to your Magento. Then follow the following steps-

1. Navigate to Stores>Settings>All Stores and create a new website than a new store and a new store view. you can use an already created store and store view the main thing you need to create is a new website.

2. Navigate to Stores>SettingsConfiguration>General>Web>Base URLs, here Base Url and Base link URL fields will be automatically filled.

Navigate to Stores>SettingsConfiguration>General>Web>Base URLs (Secure), here Base Url and Base link URL fields will be automatically filled.

3. Now you need to update these URLs for your newly created website. You’ll see a store view option on the top of the page on the left side. Open this drop-down and select your newly created website.

4. Now update the Base URL & Base Link URL fields in Base URLs and Base URLs (Secure). Save this configuration now.

5. Now you need to search for ‘$bootstrap=Bootstrap:: create (BP, $_SERVER);’ from your Magento pub/index.php file, add the following line, and save it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/**
* 注釋默認
* default websites
*/
// $bootstrap = Bootstrap::create(BP, $_SERVER);

/**
* test_new_web:後臺配置website code
* multiple websites
*/
switch($_SERVER['HTTP_HOST']) {
case 'mysite.test':
$mageRunCode = 'test_new_web';
$mageRunType = 'website';
break;
default:
$mageRunCode = 'base';
$mageRunType = 'website';
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);

6. Update local host file to add folder Linux

  • Windows hosts (C:\Windows\System32\drivers\etc)

編輯 hosts 文件

1
echo "127.0.0.1 mysite.test" | sudo tee -a /etc/hosts

or

1
2
127.0.0.1 magento2.docker
127.0.0.1 mysite.test