Second website on a single installation of MODX problem

Problem: I set up a second context for a second website hosted on the same account (to use a single MODX installation) and it insists on showing the content for the primary domain. The base_ref and canonical URLs in the HTML are printed correctly, showing the secondary domain, but the content displayed is from the primary domain. There are no error messages.

Debussy and Belafonte have written about how to set multiple sites up, but what they say doesn’t help me, and I don’t find anything in the docs.

What I did: Added the domain as an addon domain in the hosting account; fixed the DNS for the domain name; then in MODX, created the new context. The context settings are these:

error_page     99
base_url           /
http_host         www.seconddomain.co.uk
site_start          97
site_url             https://www.seconddomain.co.uk/

The new home page resource (97) exists in the second context and is published

The folder (named: greybul) for the site exists in the root, and has an index.php file set to initialize the appropriate context for the secondary domain.

The root htaccess has this added:

RewriteCond $1 !^greybul/
RewriteCond %{HTTP_HOST} ^www\.seconddomain\.co\.uk
RewriteRule (.*) /greybul/$1 [L]

RewriteCond %{HTTP_HOST} ^www\.primary-domain\.co\.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I open the second domain home page resource in the manager area and click View, and I see the unformatted content of the primary domain home page.

I missed something, obviously. I would like to ask what I missed.

You need a router plugin in MODx to switch the context.

Either use something like XRouting for that or write your own plugin.

1 Like

Sorry, forgot to mention I also followed the instruction to create a context switching plugin and set it to fire at OnHandleRequest:

/* don't execute if in the Manager */
if ($modx->context->get('key') == 'mgr') {
        return;
}

switch ($_SERVER['HTTP_HOST']) {
        case 'brandy-bank-house.co.uk':
                // if the http_host is of a specific domain, switch the context
                $modx->switchContext('web'); // Context key
                break;
        case 'grey-bull.co.uk':
                // if the http_host is of a specific domain, switch the context
                $modx->switchContext('greybull'); // Context key
                break;
        default:
                // by default, don't do anything
                break;
}

After creating a second resource in the second context, I see that clicking View, leads to a 404 error. The second context home page continues to display the home page content (unformatted) from the primary context.

What is the folder greybul in the root for? I’m not sure you need that.


Maybe you can put an error log statement into your router plugin to see what the value of $_SERVER['HTTP_HOST'] is. Maybe there is a problem there.

$modx->log(modX::LOG_LEVEL_ERROR,'HTTP_HOST = ' . $_SERVER['HTTP_HOST']);

I am following Belafonte:

https://belafontecode.com/modx-revolution-hosting-multiple-domains/

Because the new site is to have new CSS, JS files, etc., they go in a folder created in the root named: greybul. At the same time, the addon domain in the CPanel domain control section, is set to pull content from public_html/greybul

That seems to be in line with the Belafonte approach. Is it wrong?

Would have been great to have this set out step by step in the docs.

No error is being logged. As quarter-trained, makes me wonder if the plugin is firing at all.

/* don't execute if in the Manager */
if ($modx->context->get('key') == 'mgr') {
        return;
}
$modx->log(modX::LOG_LEVEL_ERROR,'HTTP_HOST = ' . $_SERVER['HTTP_HOST']);

switch ($_SERVER['HTTP_HOST']) {
        case 'brandy-bank-house.co.uk':
                // if the http_host is of a specific domain, switch the context
                $modx->switchContext('web'); // Context key
                break;
        case 'grey-bull.co.uk':
                // if the http_host is of a specific domain, switch the context
                $modx->switchContext('greybull'); // Context key



                break;
        default:
                // by default, don't do anything
                break;
}

Checked that the anonymous user has the Load_Only access policy for the second context. It does. So I assume the access permissions are not to blame.

Does it log something when you open a page on your main context? Checkbox set for OnHandleRequest? Is the plugin active?


Just skimming the Belafonte approach (that I never heard of before). There seem to be 2 options decribed and if you are choosing option 2 (with the separate folder) I’m not sure you even need a plugin (as you initialize the correct context in index.php).

Nothing is logged. So the plugin is not running/firing?

Yes, the checkbox for OnHandleRequest is ticked.

Yes, I saw Belafonte say there were two alternatives: the switching plugin or the folders with index files that intialize their respective contexts. I have both. To no avail. Although the domain name avoids triggering a 503 error, and therefore sort of works, the context is not being found. No idea why.

Uploaded a static HTML index file to the folder for the second domain name, and it displays correctly. Ergo, the domain name is configured correctly. The problem - I guess - is somehere inside MODX.

The template for the new home page contains:

<base href="[[!++site_url]]">

And the URL gets rendered correctly there, which I presume is being pulled from the context settings. But why isn’t the index number of the context home page resource being found when it has also been correctly entered in the context settings for the second context?

I believe this value is always generate from the current request and not read from the settings.


I’m still confused why your plugin won’t run. It could help to log some additional information (like current context $modx->context->get('key') or the value of q $_REQUEST['q']) to troubleshoot this.
Have you tried clearing the cache?

Yes, I have manually deleted the core/cache folder contents a number of times after making changes to the setup.

Went back to Belafonte again in the absence of any apparent documentation, and noticed the bold letters in the following:

“When the official documentation refers to a ‘Gateway Plugin’ it’s essentially referring to the ‘index.php’ file. ONLY do this if you’re not planning on giving your sites separate resource folders.

So I deleted the gateway plugin. Emptied the cache again. No change. Same as before.

Belafonte implies htaccess is crucial. And I do have doubts about the htacess for the root (grey-bull is the second domain, and brandy-bank the primary):

RewriteEngine On
RewriteBase /

#MAP TLD TO CUSTOM SUBDIRECTORY
RewriteCond $1 !^greybul/
RewriteCond %{HTTP_HOST} ^www\.grey-bull\.co\.uk
RewriteRule (.*) /greybul/$1 [L]

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.brandy-bank-house\.co\.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Tried the switch plugin again, adding a stupid echo ‘logged’; line at the top. Nothing gets echoed. So it ain’t running. Despite being set to fire OnHandleRequest.

Also tried turning off Mod_Sec because that has been the culprit so many times in the past. But no joy this time.

You said that you can successfully call a static file inside the greybul folder, so I think this .htaccess file should be ok.

Although I’m not sure what $1 is refering to in this line:

RewriteCond $1 !^greybul/

So what is the content of .htaccess inside the greybul folder? Maybe you have to adjust the RewriteBase to make it work.

RewriteBase /greybul/

Belafonte is clear about the child folder htaccess needing to be: RewriteBase / but in my desperation I have also tried /greybul/ to no avail. Same story.

Tried deleting those three lines in the htaccess in the root after you queried the first line. Put the static index file in to check. The static shows nicely. Switching back to the MODX index file returns us to the same old misery.

According to the records, BobRay said this 5 years and 11 months ago: “I usually avoid contexts if at all possible.” He recommends using separate installs of MODX. It is starting to look as if Bob is right (although I recall trying that in the past and having other headaches).

I only questioned the first line!

I think Belafonte assumes that both domains point to the same directory, so they have this rewrite rule. If your second domain already points to the greybul folder, then you probably don’t have to do any changes to the .htaccess file in the root.

What you say about Belafonte makes sense, even to an idiot like me, in which case the current setup ought to work, but it doesn’t. So going to take a break, and then have a go at a multiple install, which I guess will mean two core folders, one of which will have to be renamed. I see there is something in the docs about renaming the core folder. Will have a go since I am now at the brick wall with the contexts idea.

Thank you massively for all your help. In times like this, when the final collapse of Western Civilisation seems so imminent (and a digital implosion as rogue code goes on a mass deletion rampage cannot be ruled out), it is immensely reassuring to know that there are people out there in the unechoing vastness who can spare the time of day to help a chap out. Thank you very, very much.