Setup Multi Store With Multi Domain in Magento 2

One of Magento’s greatest strengths is its capabilities for scaling to support multi-site, multi-store and multi-language retailing all from the single admin. One instance of the Magento software can enable you to start multiple websites or store views that use different Default languages, Domain names, Categories. This feature allows you to create websites, stores as many as you need only in the single configuration. All stores share the same domain, be set up as subdomains of the same domain, or have an entirely different domain, especial, possibly share the same backend for the easier administration. Magento is one of the best solutions for multi-store functionality.

Setting-up multi-domain using vhost:

In this post, we will be exploring two separate concepts on setting up a multi-domain, One is setting multi-domain when creating VHost and setEnv on the vhost itself which can be done with the help of your hosting provider and here is the detailed and separate tutorials from Magento for apache and nginx servers. So, before we jump into the article we should know how to actually set up the multi-website, multi-store and store views within Magento itself which is explained in a much better way in the official documentation site.

Setting-up multi-domain using index.php

If your hosting provider(due to security concern) doesn’t support setEnv on vhost then it is something which can be done within Magento itself. So, once you finished setting up multiple websites, stores, and store views you can use multidomain for multi-stores by just modifying the index.php file which is located on your Magento Root. Open the index.php file and replace following code with that and change domain and website, store or store view code as explained below.

<?php
/**
 * 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 © 2016 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);
}

/** Multi Domain code begin **/
$params = $_SERVER;
$domain2store = array(
    'domain1.com'=>'default', // Replace your Website, Store or Storeview code with this.
    'domain2.com'=>'de',      // Replace your Website, Store or Storeview code with this.
    'domain3.com'=>'es',      // Replace your Website, Store or Storeview code with this.
    'domain4.com'=>'fr',      // Replace your Website, Store or Storeview code with this.
    );
if(isset($domain2store[$_SERVER['HTTP_HOST']]))
$storecode = $domain2store[$_SERVER['HTTP_HOST']];
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = isset($storecode) ? $storecode : '';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store'; 

/** Multi Domain code end **/

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

Okay, Let’s take a deeper look at the above code, We have commented on the start and end of code customization which makes easy identification of additional codes. Now, the $domain2store variable is declared with the array of values which stores the different main-level domains(multi-domains) in association with the store code, here we used the “store” code for PARAM_RUN_TYPE and hence we used it on the array declaration. you can add multiple domains and the associated store code (as shown below) separated with “,”.

$domain2store = array(
    'domain1.com'=>'default', // Replace your Website, Store or Storeview code with this.
    'domain2.com'=>'de',      // Replace your Website, Store or Storeview code with this.
    'domain3.com'=>'es',      // Replace your Website, Store or Storeview code with this.
    'domain4.com'=>'fr',      // Replace your Website, Store or Storeview code with this.
    );

After made this changes you just need to save the index.php file and upload it to your server. That’s it. Now run the domain1.com and you’ll see specific Website, Store or Storeview you assign to that store.

We hope this Magento post helped you to Setup Multi-Store With Multi-Domain in Magento 2. Bookmark it for your future reference.


One thought on “Setup Multi Store With Multi Domain in Magento 2

  1. Being a business owner myself, this blog about eCommerce strategies is quite insightful. It is one of the better sources for procuring ideas to create a better online presence.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.