3

I have Magento 2 with two store views: one with code "a" and one with code "b".

I have set up two domains mydomain.a and mydomain.b,  with public folders

/a/public_html/

/b/public_html/

now

/a/public_html/index.php looks like

<?php
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'a';
$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);

and /b/public_html/index.php looks the same except that I have 'b' in the 3rd line.

in /b/public_html/ I have set up symbolic links to all folders in /a/public_html/ (app, bin, dev, pub,...).

the "base URL" properties for store view "a" is "http://www.mydomain.a" and for "b" "http://www.mydomain.b".

But when I go to mysite.a it looks fine, but when I go to mysite.b it redirects to mysite.a and language is a, not b.
And when I go to either mysite.b/admin or mysite.a/admin it starts to loop from a to b and I get "This website has a redirect loop".

Isn't this super weird? Perhaps if I have two full installations of Meganto 2 and just a common database, would this work?

7 Answers 7

7

Maybe this will help anybody, I use this in my index.php and works like a charm :)

switch ($_SERVER['HTTP_HOST']) {
    case 'www.domain-x.com':
    case 'domain-x.com':
        $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'x'; 
        $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-y.com':
    case 'domain-y.com':
        $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'y'; 
        $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; 
}
1
  • what is Mage::run(); doing?
    – paj
    Oct 12, 2019 at 18:18
7

You don't need to edit the index.php or .htaccess files. You just need to add the ENV variables to your vhosts file after you have setup the store in the magento admin.

Here is an example vhosts file for apache 2. The main website is "mysite.mg" and two sub-sites "french.mysite.mg" and "german.mysite.mg":

<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>

The SetEnv MAGE_RUN_CODE is the code that you setup when you created the website in the magento admin and SetEnv MAGE_RUN_TYPE is "website" when you are using the website code. Here is a link for reference.

1

Using Magento 2, we can implement multi domain with single Magento 2 installation. and there is no need to create separate folders like

/a/public_html/ & /b/public_html/

You can read this docs for more clarification. Read More

1

Yes this is super weird :) Magento allow multi domain setup web site without creating two separate installations

Stores -> Settings -> All Stores

Let’s look at each of three options

Web site We need to focus on web site section of all stores. In default Magento comes with one web site (base). If you need more domains you need to create more web sites.

Websites not sharing cart, user sessions, payment gateways, shipping etc.

Store Each website have at lease one store. Stores share cart, payment gateways etc. Typically used in different product selections, menu structure etc.

Store View Stores can have one or several store views. Store views typically used in layouts and lanugages

Next you need to go to

Admin -> configuration

Set up your domain for Base Urls And Base Urls (secure) for both web sites

Then in your index.php

Open your index.php file and replace following line.

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

With following 4 lines of code

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

.htaccess changes Open your root .htaccess file and add following lines

SetEnvIf Host .*multidomain.fc5.forge.co.nz.* MAGE_RUN_CODE=base
SetEnvIf Host .*multidomain.fc5.forge.co.nz.* MAGE_RUN_TYPE=website
SetEnvIf Host .*australian.fc5.forge.co.nz.* MAGE_RUN_CODE=australia
SetEnvIf Host .*australian.fc5.forge.co.nz.* MAGE_RUN_TYPE=website

Go to #RewriteBase /magento/ and include following lines of code

RewriteCond %{HTTP_HOST} ^(.*)multidomain\.fc5\.forge\.co\.nz
RewriteRule .* – [E=MAGE_RUN_CODE:base]
RewriteCond %{HTTP_HOST} ^(.*)multidomain\.fc5\.forge\.co\.nz
RewriteRule .* – [E=MAGE_RUN_TYPE:website]

RewriteCond %{HTTP_HOST} ^(.*)australian\.fc5\.forge\.co\.nz
RewriteRule .* – [E=MAGE_RUN_CODE:australia]
RewriteCond %{HTTP_HOST} ^(.*)australian\.fc5\.forge\.co\.nz
RewriteRule .* – [E=MAGE_RUN_TYPE:website]

Find out more from full article at http://www.forgeonline.co.nz/magento-multistore-website-shopfront/

0

It could be that your site base_url is the same for both sites. This is stored in the database. Each web site will redirect to this url so if they are the same in both databases or you are using the same database you could see this situation.

0

Is no need to create other site folder, just same website folder. In my case, prepare multiple stores, and using MobileDetectLib on index.php to redirect to specific URL.

VI MagentoWebSite/index.php

if($_SERVER['SERVER_NAME'] == 'www.MagentoWebSite.com'){
    require_once 'MagentoWebSite/vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php';
    $detect = new Mobile_Detect;
    if($detect->isMobile()) {
        header('Location: http://m.MagentoWebSite.com/');
        exit;
    }
}
0
**edit in pub/index.php**

if ( $_SERVER['HTTP_HOST']=='magento.local'){
    $_SERVER[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'ps';
    $_SERVER[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
}elseif($_SERVER['HTTP_HOST']=='francemagento.local'){
    $_SERVER[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'fr';
    $_SERVER[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
}else{
    echo "webiste is not running";
}

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.