Blog

How to setup multiple website and multistore in Magento 2 on localhost

Hey, How are you? Hope you are doing well.

Welcome to the magento2 world.

Let’s understand how can we crate multiple website and multiple store in magento2 on localhost Xampp Windows.

So if you are looking for exact steps to perform above mentioned, then you are in the right page.

Introduction

Lets understand the requirement:

  1. Website sonal.fashion.com with two store views — EnglishΒ andΒ German
  2. Website sonal.grocery.com with one store view – English

 

Are you clear with the requirement?

In this process you will learn how to create multiple store for a single website and also how to create multiple website with multistore in localhost.

Follow the steps:-

Step 1: Create a Root Category
Step 2: Create a new website
Step 3: Create a new store
Step 4: Create store views
Step 5: Configure the Store URL
Step 6: Map the vhost to work with Magento 2

Step 1: Create a Root Category

We need to create root categories and its sub-categories for our stores as shown below.

 

We created

For sonal.grocery.com : –

  1. Grocery,
  2. Grocery->Fruits & Vegetables, and
  3. Grocery->Bakery & Dairy.

For sonal.fashion.com (English Default store view) :-

  1. Fashion
  2. Fashion->Men
  3. Fashion->Women

For sonal.fashion.com (German store view):-

  1. Fashion German,
  2. Fashion German->MΓ€nner
  3. Fashion German->Frauen

If you are not sure how to create these categories then follow categories creation video on youtube .

You can create categories as per your requirement.

Step 2: Create a new website

Login into Magento 2 admin, and go toΒ StoresΒ |Β All Stores. If you install a fresh Magento 2 instance that doesn’t have a store yet, you will see just one row on the Stores grid includingΒ Main Website,Β Main StoreΒ andΒ The Main Store ViewΒ which are default in Magento 2.

 

Now, we need to create websites, click on “Create Website” button and fill the information as shown below for sonal.fashion.com and Click “Save Web Site”.

 

  • Name: Enter your website name (eg. the domain of the new website)
  • Code: Enter a unique (lowercase) code for the website. The code will be used on the server to point to the domain.
  • (Optional)Β Sort Order: determine the sequence in which the site is listed with other sites. Enter a zero to make the site appear at the top of the list.

 

Now, its time to create other website that is sonal.grocery.com. Click on “Create Website” button and fill the information as shown below then Click “Save Web Site”.

 

Our websites has been created. Lets go to next step.

Step 3: Create a new store

On the Stores page (StoresΒ |Β SettingsΒ |Β All Stores), click onΒ Create Store and fill the following information as per your requirement:

  • Web Site: Assign the name of the website we just created above that is the parent of the new store.
  • Name: Enter a name to identify the store. The name is for internal reference only.
  • Code: Enter a unique code to identify the store.
  • Root Category: Set the root category for this store. The root category defines the category structure for the main menu of the store. You can come back and change the Root category later.

Default Store for sonal.fashion.com –

Select Root Category – Fashion

 

German Store for sonal.fashion.com –

Select Root Category – Fashion German

 

Default Store for sonal.grocery.com –

Select Root Category – Grocery

 

Done. Awesome we created all stores. Follow the next step.

Step 4: Create store views

On the Stores page (Stores | Settings | All Stores), click onΒ Create Store View and understand the following details:

  • Store: Set Store to the parent store of this view.
  • Name: Enter a name to identify this store view. This name will appear in the language chooser in the store header.
  • Code: Enter a unique (lowercase) name to identify this store view.
  • Status: set to Enabled option
  • (Optional)Β Sort Order: to determine the sequence in which this view is listed with other views
  • You can repeat the same steps to create new websites, stores or store views. Here are the sample multi-stores we’ve just created above:

sonal.fashion.com – default store view

 

sonal.fashion.comΒ – german store view

 

sonal.grocery.com – default store view

 

Here are our stores ready to launch.

 

Step 5: Configure the Store URL

Now its time to map the websites with the domain name.

Here’re the steps:

  • Configure the base URL

In this tutorial, we still keep the default Magento 2 Stores includingΒ Main Website,Β Main StoreΒ andΒ The Main Store ViewΒ unchanged.

Now let’s configure the Store URL for the two websites — sonal.fashion.comΒ andΒ sonal.grocery.comΒ — that we just created for testing above:

 

Magento 2 multi stores’ scope

Go toΒ StoresΒ |Β ConfigurationΒ | (General)Β WebΒ |Β Base URLs. Change the Store View scope at the top to the sonal.fashion.comΒ Website that we created above. You will be prompted when switching; press OK to continue.

Now uncheck the checkbox calledΒ Use system valueΒ from theΒ Base URLΒ andΒ Base Link URLΒ fields and commit your domain name. Now clickΒ Save ConfigΒ and continue the same procedure for the other website. The output should look like this:

 

Apply the same steps to configure the base URL for the sonal.grocery.com default store view.

 

Once done, save your entire configuration

  • Configure a Virtual Host (vhost)

For Window OS:

Open up the hosts file as an administrator (Go toΒ C:\Windows\System32\drivers\etc). Add the following (change the IP and domain name accordingly)

127.0.0.1 sonal.fashion.com
127.0.0.1 sonal.grocery.com

For Mac OS:

Open up the hosts file as a superuser (Go toΒ /etc/Β directory). Add the following (change the IP and domain name accordingly)

127.0.0.1 sonal.fashion.com
127.0.0.1 sonal.grocery.com

Save the file and run the following command on the terminal:

dscacheutil — flushcache

Depending on your Mac version, you might need to check the commands accordingly.

Map the vhost to work with Magento 2

Depending on your set up preferences, you can follow one of the following options:

Option 1: Editing the index.php file
(This option works for both NGINX and Apache server)

Open up the fileΒ index.php in your pub directory of Magento 2 root folder (Make sure you backup this file before editing), and add below code before “$bootstrap = Bootstrap::create(BP, $_SERVER);” line.

$params = $_SERVER;
$domainName = $_SERVER['SERVER_NAME'];
switch ($domainName) {
case 'sonal.fashion.com':
$runType = 'website';
$runCode = 'fashion';
break;
case 'sonal.grocery.com':
$runType = 'website';
$runCode = 'grocery';
break;
default:
$runType = 'website';
$runCode = 'base';
}
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $runCode;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = $runType;

 

NOTE:

  • Case β€˜β€™: refers to your custom testing domain
  • $runType:Β The sample setup above uses the $runType β€˜website’ variable. You may change website to β€˜store’, at your discretion.
  • $runCode:Β refers to the unique code for the website that you specify when creating your Website. This code will be used on the server to point to the domain.
    • If you setΒ $runTypeΒ β€˜website’, theΒ $runCodeΒ should be your Website code
    • If you setΒ $runTypeΒ β€˜store’, theΒ $runCodeΒ should be your Store View code

Option 2: Editing the .htaccess file:
(NOTE: this option applies for Apache server only)

Add the following code to the bottom of yourΒ .htaccessΒ file:


<IfModule mod_rewrite.c>
Options +FollowSymLinksSetEnvIf Host .*sonal.fashion.com.* MAGE_RUN_CODE=fashion
SetEnvIf Host .*sonal.fashion.com.* MAGE_RUN_TYPE=website
SetEnvIf Host .*sonal.grocery.com.* MAGE_RUN_CODE=grocery
SetEnvIf Host .*sonal.grocery.com.* MAGE_RUN_TYPE=website
RewriteEngine on
RewriteRule .* -- [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* -- [L,R=405]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>

You can replace theΒ SetEnvlf HostΒ andΒ MAGE_RUN_CODEΒ variables with your own value accordingly.

  • SetEnvlf Host: refers to your custom testing domain
  • MAGE_RUN_CODE: refers to the unique code for the website that you specify when creating your Website. This code will be used on the server to point to the domain.

 

Clear your Magento and browser cache. Then you should be able to see to the custom domains from your storefront.

Access sonal.fashion.com-

 

sonal.grocery.com

 

Summary

In this tutorial, you have got the knowledge of websites, stores, and store views.Β You have got multi-stores within a single Magento 2 installation and map those websites to custom domains.

Feel free to comment below and let me know your feedback on this.

You can also reach out to this below video to setup multistore and multiple website on the magento2.

7 thoughts on “How to setup multiple website and multistore in Magento 2 on localhost

  1. Hi Sonal. Hope you fine and good. I follow the above document you provided for setup multiple website and multi-store. But when i run any of them it shows the Apache2 Ubuntu Default Page. whats the problem. I follow all of your guidelines and commands. Please help to solve the issue i am facing.

  2. Thank you so much for your efforts.

    Instead of creating a sub-domain for each website can we call it by a subfolder name?
    e.g.
    sonal.com/fashion
    sonal.com/grocery

  3. This was a nice post. Please let me know what code level changes is needed if I want to have different design for different stores. Thanks πŸ™‚

Leave a Reply

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

πŸš€ Let’s Connect on LinkedIn! πŸš€

Want to level up your skills and knowledge? πŸš€
Follow us on LinkedIn to get:
βœ… Expert insights on business growth
βœ… Daily tips to sharpen your skills
βœ… Exclusive updates from The Coach SMB

Let's grow together!