Search code examples
magentowebstorecontrollers

Magento multi website controller


Is it possible to programmatically dispatch websites in Magento? Currently, I have a directory in the root of my site called /websites. In this directory I have subdirectories for each website, for example, site_a, site_b, site_c. Then each site subdirectory has a .htaccess and index.php with appropriate run code, for example:

$mageFilename = '../../app/Mage.php';
require_once $mageFilename;    
Mage::run("site_b", "website");

in the index.php in /websites/site_b. While this works, I do wish to avoid doing file management in the filesystem. Anyone can give any recommendation on the best way to do this? Perhaps a script in /websites that stands in place of the individual site sub directories. Any help appreciated.


Solution

  • You have access to the $_SERVER variables in php and you can use these to determine the Mage::run("whatever", "website"); call, e.g.:

    $whatever=$_SERVER['condition/url/whatever'];
    
    // or use some cookies 
    
    if (isset($_COOKIE['dev'])) $whatever=$_COOKIE['dev'];
    
    
    switch($whatever)
    { case "example.com":
      case "www.example.com":
        $_SERVER['MAGE_RUN_CODE'] = "example";
        $_SERVER['MAGE_RUN_TYPE'] = "website";
        break;
      case "dev":
      case "test.com":
        $_SERVER['MAGE_RUN_CODE'] = "test";
        $_SERVER['MAGE_RUN_TYPE'] = "website";
      default:
        $_SERVER['MAGE_RUN_CODE'] = "live";
        $_SERVER['MAGE_RUN_TYPE'] = "website";
    }
    Mage::run($_SERVER['MAGE_RUN_CODE'], $_SERVER['MAGE_RUN_TYPE']);
    

    In this way you only need one code base plus correctly configured settings in Magento backend to route to your different stores.