We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22656
    • 121 Posts
    I tried following the tutorial on http://www.belafontecode.com/modx-revolution-hosting-multiple-domains/. I have set everything up and it all worked fine (individual context's pages were showing). I undid this change as it wasnt ready for release yet, and having come back to it now I am finding switchcontext isnt working any more? What am I doing wrong?

    I have changed the code in my index.php to take into consideration multiple contexts and switch based on http_host. However, no matter what I do it seem that the default context's webpages keep showing.

    This is the code I inserted into index.php:
    /* Setup context mapping */
    switch ($modx->getOption('http_host')) {
    	case 'mytest.website.com:80':
    	case 'mytest.website.com':
    		// if the http_host is of a specific domain, switch the context
    		$modx->switchContext('mytest');
    	break;
    	default:
    		// fallback, go to main context
    		$modx->initialize('web');
    	break;
    }
    


    It would appear going to mytest.website.com works fine, however it loads the page from www.website.com!
    It is almost as if the line
    $modx->switchContext('mytest');
    doesnt do anything, or as if the if statement falls through to default.

    I have tried including the snippet on the homepage printing out what the http_host value holds, it appears to be correct even though the context itself is not switching.

    Any help?
      • 22656
      • 121 Posts
      Grumble... well I've managed to get it working. It would appear http_host variable is empty when the if statement in index.php file reaches it. Rather than relying on it, i've gone with PHP's SERVER variable, this seemed to have kickstarted things into action.

      Also, instead of using "switchcontext" i had to use "initialize" in order for the secondary context to work, so this is what I got now:

      switch ($_SERVER["SERVER_NAME"]) {
      	case 'mytest.website.com:80':
      	case 'mytest.website.com':
      		// if the http_host is of a specific domain, switch the context
      		$modx->initialize('mytest');
      	break;
      
      	default:
      		// fallback, go to main context
      		$modx->initialize('web');
      	break;
      }