Magento Tutorials

How to Setup Magento 2 with Multiple Stores and Domains

With just one single instance of the Magento software, you can have multiple websites, stores, and storeviews operating on the same Magento backend. This in turn allows for limitless flexibility for your eCommerce operations, but the process of setting up a store with multiple stores and domains can be tricky to get right—and our article today will be able to help you with this.

First, let’s take a look at an overview of the multisite scope, and we’ll process from here.

Magento multisite scope
The multi-site scope
Source: Magento

Step 1: Create websites

At the top level of your multistore scope is websites. A new website is required when you want to create a separate site that does not share cart information, shipping methods, payment methods, etc with the default website.

To create a new website:

  1. In your Magento backend, go to Stores > Settings All Stores.
magento 2 all stores menu
  1. On the Stores page, click Create Website.
    Magento 2 create website
    • Name: Enter a name to identify the website.
    • Code: Enter a unique code; for example, if you have a French store, you can enter french
    • Sort Order: Enter an optional numerical sort order.
  2. Click Save Web Site.
  3. Repeat these tasks as many times as necessary to create your websites.

Step 2: Create stores

Managed by your websites are stores. With multiple stores, you use the same product catalog to present different selections of products and categories. A store can have multiple storeviews.

To create a new store:

  1. In your Magento backend, go to Stores > SettingsAll Stores.
  2.  On the Stores page, click Create Store.
    Magento 2 create store
    • Web Site: Select the website with which to associate this store.
    • Name: Enter a name to identify the store.
    • Root Category: Select the root category for this store.On the Stores page, click Create Store.
  3. Click Save Store.
  4. Repeat these tasks as many times as necessary to create your stores.

Step 3: Create store views

Managed by stores are storeviews—the selectable views that customers can choose to enable different languages, catalog prices, and/or different currencies.

  1. In your Magento backend, go to Stores > SettingsAll Stores.
  2. On the Stores page, click Create Store View.
    Magento 2 create store view
    • Store: Select the store with which to associate this store view.
    • Name: Enter a name to identify this store view.
    • Code: Enter a unique name to identify this store view.
    • Status: Select Enabled.
    • Sort Order: Enter an optional numerical sort order.
  3. Click Save Store View.
  4. Repeat these tasks as many times as necessary to create your store views.

Step 4: Change the website’s base URL

  1. In your Magento backend, go to Stores > SettingsConfiguration. Under General section, click Web.
  2. From the Store View list at the top of the page, select a website.
  3. Expand Base URLs section.
Magento 2 base url
  1. Enter this website’s URL. For example: https://subdomain.example.com/
  2. Click Save Config.
  3. Repeat these tasks for other websites.

Step 5: Add the store code to the base URL

Magento gives you the option to add the store code to the site’s base URL, which simplifies the process of setting up multiple stores. Using this option, you don’t have to create directories on the Magento file system to store index.php and .htaccess.

This prevents index.php and .htaccess from getting out of sync with the Magento codebase in future upgrades.

  1. In your Magento backend, go to Stores > SettingsConfiguration. Under General section, click Web.
  2. From the Store View list at the top of the page, select Default Config.
  3. Expand Url Options section.
Magento 2 url options
  1. Uncheck the Use system value box next to Add Store Code to Urls. Then set this to Yes.
  2. Click Save Config.
  3. If prompted, flush the Magento cache. (System > Cache Management).

Step 6: Change the default store view’s base URL

  1. In your Magento backend, go to Stores > SettingsConfiguration. Under General section, click Web.
  2. From the Store View list at the top of the page, select Default Config.
  3. Expand Base URLs section.
  4. Enter your website’s base URL. For example: https://www.example.com/
  5. Click Save Config.

After this step, you’ll lose access to your Magento Admin. But don’t worry, your Magento Admin will be accessible after completing the next step which is configuring virtual hosts.

Step 7: Server-side configurations

To finish setting up multiple stores and domains for your Magento website, you’ll need to make some changes to your web server configuration. In short, you’ll need to:

  • Create a new virtual host for each new website
  • Pass the values of MAGE_RUN_TYPE and MAGE_RUN_CODE to the new configurations
  • (Optional) Set static routes to the host

The process of creating a new virtual host for your new websites might vary based on what type of server you’re running. For example, with Apache 2.4 and above, the process involves editing your /etc/httpd/conf/httpd.conf like so:

<VirtualHost *:80>
   ServerName          mysite.mg
   DocumentRoot        /var/www/html/magento2/pub/
</VirtualHost>

<VirtualHost *:80>
   ServerName          french.mysite.mg
   DocumentRoot        /var/www/html/magento2/pub/
   SetEnv MAGE_RUN_CODE "french"
   SetEnv MAGE_RUN_TYPE "website"
</VirtualHost>

<VirtualHost *:80>
   ServerName          german.mysite.mg
   DocumentRoot        /var/www/html/magento2/pub/
   SetEnv MAGE_RUN_CODE "german"
   SetEnv MAGE_RUN_TYPE "website"
</VirtualHost>
  • ServerName: is the new domain for your new website 
  • DocumentRoot: where Magento is served. It’s recommended that you should set DocumentRoot to Magento /pub/ directory for security purposes
  • SetEnv MAGE_RUN_TYPE: set to either website or store, depending on if ServerName is a new website or a new store scope
  • SetEnv MAGE_RUN_CODE: set to the unique website or storeview code that corresponds to MAGE_RUN_TYPE

After you’re finished configuring, restart your web server by executing:

  • CentOS: service httpd restart
  • Ubuntu: service apache2 restart

(Optional) Set static route(s) to the host

In case you don’t have DNS set up for your store’s URLs, you’ll need to set up static routes in your hosts file with the following format:

<ip address> <new website>

For example:

192.168.1.15 simicart-store.com
192.168.1.16 french.simicart-store.com
192.168.1.17 german.simicart-store.com 

Notes: Set your Magento domains with distinct IP addresses if you want a separate checkout process

And it’s done! Now go to the URLs that you have set up for your new store/website and see if everything works properly.

Related post: How to Add and Configure Multi Language for your Magento Store

Is it helpful?

A knowledge craver who always strive to be wiser everyday.

Subscribe
Notify of
guest
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Samuel
2 years ago

Saved me a lot of time here, thanks for your simple and clear guide!

Manju
Manju
2 years ago

Your steps helped me too.. Thanks! a lot.

prashanth nagpal
2 years ago

Hi Dom, I am not into tech but i want my team to do the same on magento one. I have two url’s (both are operated and managed separately) by the same person. I wanted to know if i can use both as one website where customers are redirected and products are uploaded as per geography, but at the same time the backend should be only a single operational panel? Hope i have been able to explain my question.
http://www.mirrorwhite.com and http://www.mirrorwhite.in

Adnan Khaliq
Adnan Khaliq
2 years ago

This post is helpful but I think I thing is missing that what to do with domains and their hosting in Cpanel.

Tom D
Tom D
2 years ago

Hey, i really cant find the this ” you set up virtual hosts as discussed in the web-server-specific topics.”
Can you post a link 🙂
T

Doug
Doug
1 year ago

Hi, thanks for this – I have a question: If I want my websites or even store views to run on different domains, how do I stop Magento redirecting them all to the default?

e.g.

default store: http://www.mystore.com – store code “us”
second store: http://www.mystore.eu – store code “eu”

I want to make sure that if a customer goes to https://www.mystore.eu they end up on https://www.mystore.eu/eu, but Magento redirects to the default store at https://www.mystore.com/us. Do I need to override this in Apache?

Lanita Thomas
Lanita Thomas
1 year ago

Yea, I’m not understanding.. I just want two different stores with two different domain names without doing a bunch of coding? If i have a store called ” http://www.lovelide.com” I want it to be pointed to that exact domain and if I have another called http://www.dontcare.com i want it to go to ” dontcare.com and they are COMPLETELY separate. I thought it would be as simple as just updating the urls to have it be directed to the domain you want? The code stuff is KILLING ME … I don”t even know where to put this “php” and “htaaccessfile” stuff..… Read more »