Skip to main content

Adobe Commerce/Magento 2 Subfolder and Subdomain Setup in Localhost for Multistore

By |October 25, 2022 December 16th, 2022No Comments

Adobe Commerce (previously Magento) allows you to configure multiple websites for a single installation.

For convenience, Adobe Commerce offers features that let you create multiple stores and manage them from the backend. But the challenge is whether you want to utilize Adobe Commerce for many stores, multiple domains, or multiple stores on a single domain.

This Blog helps you to set the possible ways to set up subfolders and subdomains i.e multi-website setup in your local system.

For Example:

You can manage this as a subfolder

 Website A     Store A    Store View A  => http://localhost/magento/

 Website B     Store B    Store View B  => http://localhost/magento/dckap

                               OR

You must have 2 different domains to setup multi-websites. like- www.b2c.com & test.b2b.com

Website A     Store A    Store View A  => http://b2c.magento.com/

 Website B     Store B    Store View B  => http://b2c.magento.com/

Method 1: Create a subfolder in localhost

After creating your new website/store/store view

Step 1: Create the folder in Magento root folder with the name of the created website code(mine is dckap), copy and paste the index.php and .htaccess from the root folder to the created folder.

To get the website code – In the Admin, click Stores > All Stores(get the website code)

Step 2: Modify the index.php as follows

<?php

/**

 * Public alias for the application entry point

 *

 * Copyright © Magento, Inc. All rights reserved.

 * See COPYING.txt for license details.

 */

use Magento\Framework\App\Bootstrap;

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;

http_response_code(500);

exit(1);

}

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = ‘dckap’; //Website code as same in admin panel

$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);

Step 3: Do the below configuration in the admin dashboard

In the Admin, click Stores > Settings > Configuration > General > Web(Select the website scope)

How to Magento 2 Subfolder and Subdomain Setup in Localhost for Multistore: Base URLs

Step 4: Now access the website in browser

 Website 1: http://localhost/magento/

 Website 2: http://localhost/magento/dckap/

How to Magento 2 Subfolder and Subdomain Setup in Localhost for Multistore: Portal

Method 2: Create a subdomain in localhost

Step 1: Open the folder virtual host cd /etc/apache2/sites-available/

Step 2: Create a file with .conf type to setup the virtual host

sudo touch magentoinstance.conf

Step 3 : Open and write the below code and save

<VirtualHost *:80>

ServerName b2c.magento.com

DocumentRoot /var/www/html/magento/pub

   <Directory “/var/www/html/magento/pub”>

  Options Indexes FollowSymLinks

  AllowOverride All

         Order allow,deny

         Allow from All

</Directory>

 SetEnv MAGE_RUN_CODE base

  SetEnv MAGE_RUN_TYPE website

</VirtualHost>

<VirtualHost *:80>

ServerName b2b.magento.com

DocumentRoot /var/www/html/magento/pub

   <Directory “/var/www/html/magento/pub”>

  Options Indexes FollowSymLinks

  AllowOverride All

         Order allow,deny

         Allow from All

</Directory>

 SetEnv MAGE_RUN_CODE dckap

  SetEnv MAGE_RUN_TYPE website

</VirtualHost>

Note: base,dckap refers to the website code 

How to Magento 2 Subfolder and Subdomain Setup in Localhost for Multistore: Create subdomain in localhost

Step 4: Open the host sudo nano /etc/hosts and write the below code and save

127.0.0.1 b2c.magento.com

127.0.0.1 b2b.magento.com

Step 5: sudo a2ensite magentoinstance.conf

Step 6: sudo service apache2 restart

Step 7: Change Base url in core_config_data table and cache clean

Step 8: Now Access the site in frontend

http://b2c.magento.com/

http://b2b.magento.com/

There you go!

Related read: How to create a Magento 2 Popup By Extending JS components & Knockout JS

Vijayashanthi M

Author Vijayashanthi M

Vijayashanthi M is a passionate coder with an experience of 2.5 years in PHP, Yii 2, and Magento 2. She is always on the lookout for interesting things on the internet, and is an avid learner of new technologies. Her hobbies include dancing and listening to music.

More posts by Vijayashanthi M

Discover What You’re Missing

Get the weekly email full of actionable ideas and insights you can use at work and home.