How to set up a Multi-store on Magento 2

Multiple store views and websites are all about running multiple different websites from the same code and database and attaching the domain names to these sites.

Step 1: Add the domain names to the Servebolt Control Panel

We use our domains magtest1.servebo.lt and magtest2.servebo.lt for our multi-store configuration. These subdomains are added to the Servebolt Control Panel, after which DNS is updated to point to the Bolt. When this is done, these domains connect to the Magento installation on this Bolt.

If you want to run the Magento Backend on a separate domain, the easiest way to achieve that – is to make the admin domain you want to use, the default store in Magento. Changing this after the initial setup can be difficult, so this is a good time to do this right. We therefore add the magadmin.servebo.lt domain here, for that purpose.

Step 2: Configure the default site, which also is the admin domain

If you have decided to use a separate admin domain, the first step is to configure Magento with the wanted admin URL as the default.

$ magerun2 config:store:set web/unsecure/base_url https://mageadmin.servebo.lt/$ magerun2 config:store:set web/secure/base_url https://mageadmin.servebo.lt/$ magerun2 cache:flush

In our case, we can now log into admin on https://magadmin.servebo.lt/admin_1esff4

When you have the admin on a separate domain, this also allows you to protect it better, for example by adding Cloudflare Access to your Servebolt Cloudflare Business plan.

Step 3: Configure stores in Magento

You can choose between adding websites or stores in Magento. A website can contain multiple stores, and a store can contain multiple store views. For this exercise, we’ll use the default website and store, and add two more store views to it.

Go to Stores > All Stores > Create Store View

We added two store views, one for our English store, and one for our Dutch store.

Go to Stores > Configuration > Set Scope and select the Store view you want to configure

After that, go to Stores > Configuration > Web and configure the URLs to use.

We now want to set both the Base URL and the Secure Base URLs for our stores to magtest1.servebo.lt and magtest2.servebo.lt. We have to customize and update both the Base URL and Base Link URL for both.

At this point, you can customize these store views independently in the admin. A last step is required though, to be able to visit these sites from your browser.

Step 4: Activate store views in .htaccess

With .htaccess we define the context for our Magento installation, and what stores or websites to run for the various domains. 

MAGE_RUN_CODE sets which website or store code to trigger. We named our store view MAINEN.

MAGE_RUN_TYPE can be either a website or a store. In our case, we added a store view, so then it should be set to store.

# Activate our English store
SetEnvIf Host magtest1.servebo.lt MAGE_RUN_CODE=MAINEN
SetEnvIf Host magtest1.servebo.lt MAGE_RUN_TYPE=store

# Activate our Dutch store
SetEnvIf Host magtest2.servebo.lt MAGE_RUN_CODE=MAINNL
SetEnvIf Host magtest2.servebo.lt MAGE_RUN_TYPE=store

It is good practice to add this to the very top of your .htaccess, which makes it easy to find and update in the future.

Step 5: Check that everything works

First, flush the caches to activate all changes

$ magerun2 cache:flush

After that, visit the different URLs you have set up to validate that it is working like expected.