Back to Top

Create multiple websites in Magento2 and access them with different domains

Updated 29 February 2024

In this blog we will be learning how to create multiple websites in Magento2 and access them with different domains.

The magento2 provides a functionality in which you can configure different websites from same magento instance. In this blog, we will see how to do the same!

We will be mapping two domains i.e.

https://www.myprimarydomain.com and
https://www.mysecondarydomain.com

1. Install the magento and configure the server normally for the main site https://www.myprimarydomain.com

Searching for an experienced
Magento 2 Company ?
Find out More

2. Now, create a new website, store and store view under Admin > Stores > Settings > All Stores

3. Go To Admin > Stores > Settings > Configuration > General > Web

4. This is the most important step as not selecting the secondary store, you can loose the access to the admin panel of the site.

Select secondary store from the store configuration at the top:


5. Configure the Base Url and the secure Base Url according to https://www.mysecondarydomain.com and save the config.

6. Now create a sub-directory(any name, lets say it secondary) at the root of the magento2 installation.

7. Copy the index.php and the .htaccess files from the root of the Magento2 to the sub folder.

8. Replace the code in index.php:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

/** @var \Magento\Framework\App\Http $app */

$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);

$bootstrap->run($app);

WITH

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'secondary'; //Webite code as same in admin panel which we entered in step 2

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

And Code:

require __DIR__ . '/app/bootstrap.php';

WITH

require __DIR__ . '/../app/bootstrap.php';

9. Now comes the role of the server management. You need to map the domain https://www.mysecondarydomain.com to /directory of magento installation/subfolder which we just created

In my case it is: /var/www/html/secondary

10. Now you need to create symlinks inside the new folder(secondary) to allow the secondary site access the CSS files and other static content.

Execute the following commands using the terminal in the secondary folder:

ln -s /home/example/example.com/html/app/ app

ln -s /home/example/example.com/html/lib/ lib

ln -s /home/example/example.com/html/pub/ pub

ln -s /home/example/example.com/html/var/ var

11. Clear Magento cache, generated, deploy static content at the root of the magento installation and check the site at https://www.mysecondarydomain.com

. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home