MODX Context Setup for Multi-site
MODX contexts allow managing multiple sites from one installation or splitting a site by languages, subdomains, protocols. Each context has its own settings, root resources, and switching keys.
Context Use Cases
Multi-site: ru.company.com and en.company.com — two contexts with different root resources.
Multi-language: one domain, different URL prefixes /ru/ and /en/.
Mobile version: m.company.com — separate context with simplified templates.
Admin vs Public: built-in mgr context — manager. web context — public site.
Creating New Context
System → Contexts → Create → key en (Latin only, brief).
After creation configure context parameters:
Key: en
Name: English Version
Settings:
base_url: /en/
site_url: https://yourdomain.com/en/
site_start: 55 (ID of root resource for English version)
error_page: 56 (ID of 404 page for this context)
default_template: 3 (default template)
cultureKey: en
locale: en_US.UTF-8
Context Switching Plugin
// Plugin: ContextSwitcher
// Event: OnHandleRequest
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
// By domain
$contextMap = [
'ru.company.com' => 'ru',
'en.company.com' => 'en',
'by.company.com' => 'by',
];
if (isset($contextMap[$host])) {
$contextKey = $contextMap[$host];
if ($modx->context->key !== $contextKey) {
$modx->switchContext($contextKey);
}
return;
}
// By URL prefix (one domain)
$prefixMap = ['/ru/' => 'ru', '/en/' => 'en', '/uk/' => 'uk'];
foreach ($prefixMap as $prefix => $contextKey) {
if (strpos($uri, $prefix) === 0) {
if ($modx->context->key !== $contextKey) {
$modx->switchContext($contextKey);
}
return;
}
}
Nginx Configuration for Subdomains
server {
listen 443 ssl http2;
server_name ru.company.com en.company.com by.company.com;
root /var/www/company.com;
# One MODX, different contexts by host
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_HOST $host; # pass host to PHP
include fastcgi_params;
}
}
Resources in Context
Each resource is created in specific context. When creating resource: Document Settings tab → Context → select needed.
// Programmatically create resource in specific context
$resource = $modx->newObject('modDocument');
$resource->set('pagetitle', 'About Us');
$resource->set('alias', 'about-us');
$resource->set('context_key', 'en'); // context binding
$resource->set('template', 3);
$resource->set('parent', 55); // context root resource
$resource->save();
Cross-context Links
[[- Link to resource in another context ]]
[[~42? &context=`en`]]
[[- Language switcher in chunk ]]
[[- (via LangSwitch snippet or manually) ]]
Settings Synchronization Between Contexts
MODX system settings have priorities: Global > Context > Namespace. Context settings override global for specific context.
// Get setting considering current context
$siteUrl = $modx->getOption('site_url'); // returns current context setting
Timeline
Setting up two contexts (ru/en) with multi-language resource structure and language switcher — 2–3 days.







