1

I have created a sub-website, sub-store, and sub-store-view from magento2 admin and update sub-store base url from configuration setting.

After that I have created a subdomain (sub.example.com), but here I am little confused that subdomain will link to root directory or any sub directory, and what changes I have to make in index.php and .htaccess file?

3 Answers 3

1

Update root/pub index.php file after created stores and set domain for stores and websites

$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);
$params = $_SERVER;
$host = $params['HTTP_HOST'];

if($host == "{domain1.com}")
{
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '{website_code1}';
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
}

else if($host == "{domain2.com}")
{
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '{website_code2}'; // Website Code
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; // website
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
}
elseif($host == "{domain3.com}") // For specific store view domain 
{   
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '{storeview_code1}'; // Store view code
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store'; // For Store
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
}
else
{
    $params = $_SERVER;
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'base';
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
}
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);
0

I have created successfully sub website in magento2, steps are as follows :

1) Create "Sub Website/Sub Store/ Sub Store View" from admin panel.

2) Update configuration setting :

base Url (http://sub.example.com/)
Link Url (http://sub.example.com/)
Static Url (http://example.com/pub/static/)
Media Url (http://example.com/pub/media/)

3) Create a sub directory 'sub' in magento root directory and point "sub domain (sub.example.com)" to this directory.

4) Copy index.php and .htaccess to this directory from root and made two changes in index.php

i) replace "/app/bootstrap.php" to "/../app/bootstrap.php"

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

to 

$params = $_SERVER;$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = "sub";
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = "website"; 
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
0

Update index.php in your magento root:

Replace:

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

with the following code:

switch ($_SERVER['HTTP_HOST']) {
    case 'www.domain.co.uk':
        $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'uk-store-code'; 
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; 
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); 
        $app = $bootstrap->createApplication('Magento\Framework\App\Http'); 
        $bootstrap->run($app); 
        break; 
    case 'www.domain.fr':
        $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'fr-store-code'; 
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; 
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); 
        $app = $bootstrap->createApplication('Magento\Framework\App\Http'); 
        $bootstrap->run($app); 
    break; 

    default: 
        Mage::run(); 
    break; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.