MODX Contexts Setup for Multi-Site

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

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.