Second website on a single installation of MODX problem

As a last-ditch effort you could try to create another plugin on the event “OnMODXInit” that logs some stuff and maybe helps to find out what’s going wrong inside MODx. (Maybe the plugin runs if the event is not “OnHandleRequest” :wink:)

<?php
$modx->log(modX::LOG_LEVEL_ERROR,'current context_key = ' . $modx->context->get('key'));
$modx->log(modX::LOG_LEVEL_ERROR,'HTTP_HOST = ' . $_SERVER['HTTP_HOST']);
$modx->log(modX::LOG_LEVEL_ERROR,'q = ' . $_REQUEST['q']);

When you have 2 MODx installations in different folders you don’t have to rename the core.

1 Like

The true idiot demonstrates his idiocy, when he fixes something, by being completely incapable of explaining why it is now fixed while before it was broken. That is the position I am now in. Basically, I just went away and made a cup of tea. You suggested adding those three lines to the switch plugin. I did that and saw all the logs coming through correctly. Of course, that doesn’t really change the fundamentals, and just adds stuff to the log, but when I emptied the cache again - BAM!!! - BINGO!!! - the right page starts showing.

Why?

Was it those three lines?

Was it the tea?

Did the code just need ten mins or so to kind of settle in and find a comfortable spot on the chip?

Absolutely no idea.

If I understood, I would volunteer to write it up for the docs. But I don’t have a clue.

Anyway, problem solved. You have been a great help. Thank you.

1 Like

To be fair, I tend to avoid contexts, but many people use them successfully.

1 Like

Thank you very much, Bob. I thought I would set up a second installation of MODX on the same account just to compare the workflow and the amount of time and pain involved. Renamed the core folder and had the second installation up within 10 minutes. Compared to about 5 hours of acute pain yesterday. The blood has still not dried on my forehead and the keyboard is a write-off.

I had used contexts in the past for a multilingual site without too much hassle, but that was one site with one domain name. With addon domain names (as now) I will stick with multiple installations. Life is too short and the base level of pain is high enough already.

Thanks.

Although I am in no position to presume to be able to help others, here is what I believe worked for me yesterday (ignoring all the hideous trial and error and brute force).

Procedure

Objective: Adding a second domain to a hosting account and using a second context so the two sites use the same MODX installation and the same database:

  1. Create AddOn domain and point it to a folder in root: public_html/seconddomain

  2. From the root, copy the three files to the seconddomain folder:
    .htaccess
    config.core (unaltered)
    index.php

  3. Choose a name for the second context and edit the line in the index,php file to initialize the correct context:
    $modx->initialize(‘newcontextname’);

  4. Create new context with these settings (using the appropriate resource ID numbers for error pages and site start, and appropriate media source number):
    base_url /
    error_page 99
    http_host www.seconddomain.co.uk
    site_start 97
    site_url https://www.seconddomain.co.uk/
    default_media_source 3

  5. Create the new media source.
    base_path /home/username/public_html/newdomain/photos/
    rel No
    base_url https://www.seconddomain.co.uk/photos/
    rel No

  6. Create a plugin (not snippet) to switch between contexts:

<?php
if ($modx->context->get('key') == 'mgr') {
        return;
}

switch ($_SERVER['HTTP_HOST']) {
        case 'primarydomain.co.uk':
                $modx->switchContext('web'); 
                break;
        case 'seconddomain.co.uk':
                $modx->switchContext('newcontextname'); 
                break;

}

In plugin settings, tick box for OnHandleRequest

  1. Bear in mind that access permissions might need checking to ensure anonymous can load the new context.

  2. Add new media source to image TVs used by new templates.

  3. Empty core/cache.

Should now work. If not, consider multiple installations (easier to set up).

1 Like

usually, you don’t need that step. You can handle all domains with one index.php in one folder. And you don’t need special rules in the .htaccess. The xrouting plugin can handle the context - switch for you.

But I didn’t read all posts in that thread.

Thanks for that. But the process of adding an AddOn domain in CPanel involves creating/specifying a subfolder for the added domain. Are you saying there is a way of just ignoring that subfolder, leaving it empty?

the domain just needs to point to the same folder as the other domain.
It it autocreates a folder, you could just ignore that one or maybe remove that unused folder

Also, in this case the files for the second site (css, js, etc.) will be different from the first site, although some of them will have the same name (main.css e.g.). In this case, it helps to put all the new stuff in a folder.

To idiots like me, it also makes a sort of sense to point the domain name to a folder and then have an index file there that fires the right context.

Thanks for that clarification. As things stand, I have scrapped the contexts and decided to go with the multiple installation approach. That has now been set up and is working fine.

When I next have to put two sites on one account, if I feel I have four or five hours to spare, I might have another go with the contexts approach and try what you suggest, running both contexts from one index file.

Thanks for point out that possibility.

Keep in mind, when you upgrade MODX, you have probably also to upgrade the second index.php

1 Like

I’ve done lots of sites using multiple contexts. I’ve got 10 in one particular setup; some are multi-language and some are mutli-domain. I’ve never had a problem. I’ve used the “Belafonte” setup on some sites, and the ContextRouter plugin on others.

As bruno said, cPanel MUST point the second domain to the same folder as the main domain, not a subfolder. Did you do that before you decided against the multicontext setup? Your site templates can refer to CSS and JS files in whatever subfolder you want for that particular site.

1 Like

Thanks for that. What I have learnt here is that an addon domain can point to the same root and index file as the primary domain. That never occured to me as a possibility.

CPanel autofills the document root field when you create an addon domain name if you try to leave it blank so the two domains point to the same root. But now that I check, I see that you can edit the auto-generated folder that CPanel creates, delete it, and leave the two domains pointing to the same root.

Of course, Belafonte, back in the happy days of 2011, did not say the new domain MUST point to the same root. He made it optional and described two options. In my ignorance, I went for option 2, where each domain has its own folder and there are two index files that fire their respective contexts. Seemed simpler at the time.

Someone needs to write it up properly and clearly for idiots like me. Belafonte did a good job, but it clearly needs improving, so ignorant chaps who want to learn can learn.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.