Magento – magento 2 multiple store views with sub domain url

magento2multistorestore-view

I am trying to have two different store views with different themes but has the same catalog and settings.

default store view: website.co.uk
second store view should have: migrate.website.co.uk
enter image description here

Both the store views share the same website and store.
it is only the front end which changes.

I have created a sub domain of the main domain
I have copied .htaccess and index.php from website.co.uk to migrate.website.co.uk and modified index.php file as below:

<?php
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'migrate';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
?>

replacing

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

.

I have also added two lines at the start of htaccess file:

SetEnvIf Host .migrate.website.co.uk. MAGE_RUN_CODE=migrate
SetEnvIf Host .migrate.website.co.uk. MAGE_RUN_TYPE=store

when check the migrate.website.co.uk it shows me a blank page with 500 error.
what is wrong in the code?

Best Answer

I wouldn't recommend modifying the index.php.

This is a core file, meaning it'll simply get overwritten the next time you update Magento.

Since you're using .htaccess files I'm assuming you run apache.

In that case, you can use a virtual host configuration to share the website's files in the same directory:

<VirtualHost *:80>
    ServerName          website.co.uk
    ServerAlias         "www.website.co.uk"
    DocumentRoot        /path/to/your/magento2/
    SetEnv MAGE_RUN_CODE "default"
    SetEnv MAGE_RUN_TYPE "store"
</VirtualHost>

<VirtualHost *:80>
    ServerName          migrate.website.co.uk
    DocumentRoot        /path/to/your/magento2/
    SetEnv MAGE_RUN_CODE "migrate"
    SetEnv MAGE_RUN_TYPE "store"
</VirtualHost>
Related Topic