Home
/
Other Applications
/
How to configure your Magento application to work as a Multistore?

How to configure your Magento application to work as a Multistore?

The primary use of the Magento multistore functionality is to create multiple stores accessible on different URLs under the same Magento installation. All stores under the Magento multistore installation share the same backend, which makes the administration easier.

The first step in setting up a multistore is to open up the Stores > All stores menu under Settings.

This would open up the available stores on your website. To add a new one, click on Create Website.

You would be redirected to a new page where you have to add the new website’s information.

  • Name – here you should add the new website’s name;
  • Code – this will be the web site’s code and you should make sure it is a unique string;
  • Sort order – this field is optional and indicates the order of your store’s priority.

When you fill in the necessary information – click on Save Web Site. This would get you back to the Stores screen where you should click on Create Store now. This would take you to a new page where you should specify your store’s details:

  • Web Site – here you should select which website will be used for the new store. For the purpose of this article we will be using the My second website web site;
  • Name – in this field you should input your store’s name;
  • Code – this will be the store’s code;
  • Root Category – here you may choose a category that the newly created store would show.

When ready, click on Save and you would be redirected to the stores’ page again. There you may see that the newly create store was successfully assigned to the website.

The next step would be to create the Create Store View by clicking on the corresponding button. This would open up the store view addition page where you should fill in the following details:

  • Store – the store to which we want to assign the view
  • Name – the store view name
  • Code – this will be our store view code
  • Status – choose to disable or enable the store view
  • Sort order – this field is optional and indicates the order of your store view’s priority.

When ready, click on Save Store View.

Then you should make sure to change the new store’s URLs via Stores > Configuration under Settings > expand General> WebAfter that from the drop-down for store view choose the newly created store and update its URLs accordingly. The next step is to configure the changes on the server.

Parked Domain Method

Add your domain as a parked domain through your Site Tools to use this method. Once the domain is added, open the index.php file of your Magento and find the lines with the following code and remove them:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

On their place input the following lines:

switch($_SERVER['HTTP_HOST']) {
case 'yourmainstore.com':
case 'www.yourmainstore.com':
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'StoreID';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
break;
 
 
case 'yoursubstore.com':
case 'www.yoursubstore.com':
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'StoreID';
$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);

In the above code, you should make sure to substitute the yourmainstore.com with your store’s domain, and yoursubstore.com – with the parked domain which will be your sub-store. Also, the StoreID should be replaced by your store’s ID number. You may verify your store’s ID when you go to Stores > All stores and open up the desired Store view. You would notice the following part of the URL:

/store_id/2/

In this case, the store ID is 2. If the store ID is 3, the aforementioned part in the URL would be /store_id/3/.

Subdomain Method

Add your domain as subdomain through your Site Tools. This will create a separate document root directory for that domain. In this case, it will be sub.domain.com and it will be inside the public_html folder. Once the domain is added, you need to establish an SSH connection to your account. Once connected, go to the directory that we previously created for the subdomain. In our case, the command is:

cd www/subdomain.com/public_html

Copy the index.php and the .htaccess files of your Magento installation to that directory:

cp ../../domain.com/public_html/index.php ../../domain.com/public_html/.htaccess .

Open the new index.php file 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::class);
$bootstrap->run($app);

with

$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'StoreID';
$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);

where StoreID should be replaced by your store’s ID number. You may look up your store’s ID when you go to Stores > All stores and open up the desired Store view. You would notice the following part of the URL:

/store_id/2/

In this case, the store ID is 2. If the store ID is 3, the aforementioned part in the URL would be /store_id/3/.

Once you add the code, save the file and close it. The last step is to create symbolic links that will point to the core Magento directories. You could do that by executing the following commands in the document root directory of your subdomain:

ln -s ../../domain.com/public_html/app/ app
ln -s ../../domain.com/public_html/lib/ lib
ln -s ../../domain.com/public_html/pub/ pub
ln -s ../../domain.com/public_html/var/ var

Note that depending on where the document root directory of your domain is, the path in the commands may defer. Once the links are created, clear the cache of your Magento and you should be able to access the newly created store with its domain name.

Share This Article