We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10473
    • 44 Posts
    Hi there,

    It might be a bit simplistic for some of you, but I’ve made a snippet which allows you to implement multi-site in a rather simple manner.

    1. Set up a MODx Site as normal. We shall call this site site1.com

    2. In the config turn friendly URLs on, as well as Use Friendly alias Path (don’t forget your .htaccess).

    3. Now make a domain alias site2.com which points to the same folder. If this is not possible, you could always use symbolic links to have two sites basically pointing at the same files.

    4. In the manager, make the following page structure (The pages listed by alias, because that’s what I will be using in the snippet later on):

    multi_home
    |
    -site1.com
    |
    -home
    |
    -site2.com
    |
    -home


    The content of multi_home should be the following snippet link:

    [!redirect_site_child!]

    and the code of redirect_site_child is:


    <?php
    $query = "SELECT id FROM modx_site_content WHERE alias=’{$_SERVER[’HTTP_HOST’]}’";
    $res = $modx->db->query($query);
    $row = $modx->db->getRow($res);
    $modx->runSnippet(’FirstChildRedirect’, array(’docid’ => $row[’id’]));
    ?>


    NOTE: Please forgive me for not using MODx goodness for table prefixes, etc, but I couldn’t be bothered wink

    And there you have it. Go to site1.com, and you will be redirected to site1.com/site1.com/home.html and with site2, you will be redirected to site2.com/site2.com/home.html.

    Other ideas would be to have reference table, so that you could use another alias instead of the site name, or automatically strip the .com off the end. I’ll leave that to someone else.


      • 25663 MODX Staff
      • 12,272 Posts
      Very cool and simple idea Gareth ... thanks!
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 27376
        • 576 Posts
        Very cool and so simple! Here’s the snippet using "MODx goodness" for prefixes smiley
        <?php
        $tblsc = $modx->getFullTableName('site_content');
        $alias = $_SERVER['HTTP_HOST'];
        
        $query = 'SELECT id FROM ' . $tblsc . ' WHERE alias=\'' . $alias . '\'';
        $res = $modx->db->query($query);
        $row = $modx->db->getRow($res);
        
        $modx->runSnippet('FirstChildRedirect', array('docid' => $row['id']));
        ?>
        Enjoy!