This second article in the Magento 2 multi-stores tutorial shows how you can create multiple websites, stores, and store views in Magento 2. Check out Part 1 to catch up on the topic.
For the purpose of this tutorial, let’s assume that we need to create a Magento 2 multi-store structure including:
- The 1st Website (samplefashion.com) with two store views -- English and German
- The 2nd Website (samplekidstore.com) with one store view -- Kidstore
Multiple websites or stores may seem like a daunting process at first but it is really quite simple. The following steps will guide you through setting up such multi stores on Magento 2.
- Step 1: Create a new website
- Step 2: Create a new store
- Step 3: Create store views
- Step 4: Configure the Store URL
- Step 5: Create a Root Category
1. Create a new website (samplekidstore.com)
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.
First, click on Create Website and commit the following details:
- 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.
2. Create a new store
On the Stores page (Stores | Settings | All Stores), click on Create Store and commit the following details:
- 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.
3. Create store views
On the Stores page (Stores | Settings | All Stores), click on Create Store View and commit 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:
4. Configure the Store URL
The next important step is to map the websites with the domain name.
Within this tutorial, let’s assume that you try testing multi-websites on your localhost environment, pointing to two testing domains:
- www.samplefashion.com (for Default store view and German store view)
- www.samplekidstore.com (for Kidstore view).
Here’re the steps:
4.1 Configure a Virtual Host (vhost)
Depending on whether you are using Windows or Mac OS, apply the options accordingly:
Window: 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)
Mac OS: Open up the hosts file as a superuser (Go to /etc/ directory). Add the following (change the IP and domain name accordingly)
Save the file and run the following command on the terminal:
Depending on your Mac version, you might need to check the commands accordingly.
4.2 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 Magento 2 root folder (Make sure you backup this file before editing), and replace the whole code with:
- 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:
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.
4.3 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 -- www.samplefashion.com and www.samplekidstore.com -- that we just created for testing above:
Go to Stores | Configuration | (General) Web | Base URLs. Change the Store View scope at the top to the samplekidstore.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 www.samplekidstore.com website.
Once done, save your entire configuration and clear your Magento and browser cache. Then you should be able to surf to the custom domains from your storefront.
5. Create a Root Category
Now go to Catalog | Categories and click on Add Root Category as follows:
Setting up a catalog structure is not difficult, but needs to be well thought out. You may add additional information here at your discretion.
Save your configuration and go to Stores | All Stores and change the store to the appropriate Root Catalog we just created.
You have now created a multi-stores within a single Magento 2 installation and map those websites to custom testing domains. Most important, you also understand the difference between the three levels -- websites, stores, and store views, so you have a good idea how the multi-stores are structured in Magento 2.