WEBcoast Logo

Multiple domains on the same page tree/site

In one project, the customer wanted to do a branch split by using the same site and page tree for both brands with 99% identical content. I tried to find a way to make TYPO3 respond on both domains with the same page tree and decide the page and content individually. After some research I came with the following solution.

It is actually possible to use environment variables in the site's base URL like

base: 'https://%env(TYPO3_BRAND_VARIANT)%.de/'
baseVariants:
  -
    base: 'https://stage.%env(TYPO3_BRAND_VARIANT)%.de/'
    condition: 'applicationContext == "Production/Staging"'
  -
    base: 'https://dev.%env(TYPO3_BRAND_VARIANT)%.de/'
    condition: 'applicationContext == "Production/Dev"'
  -
    base: 'https://%env(TYPO3_BRAND_VARIANT)%.ddev.site/'
    condition: 'applicationContext == "Development"'

I used the `public/.htaccess` to set the environent variable based on the host name:

SetEnvIf Host "(?:(?:stage|dev)\.)?one-brand\.(?:de|ddev\.site)$" TYPO3_BRAND_VARIANT=one-brand
SetEnvIf Host "(?:(?:stage|dev)\.)?the-other\.(?:de|ddev\.site)$" TYPO3_BRAND_VARIANT=the-other

So far so good. The problem is, that TYPO3 caches the generates site configuration after the environment variables have been replaced. This led to, that I only could access the page or domain I opened first and therefore was written into the site configuration. After the page configuration was cached, the other domain threw a 404. To solve this, I needed to XCLASS the `SiteConfiguration` class.

// Override the SiteConfiguration class
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Core\Configuration\SiteConfiguration::class] = [
    'className' => \MyVendor\Sitepackage\Configuration\SiteConfiguration::class,
];

In my custom `SiteConfiguration` class I simply changed the cache identifier and appended the hash of the environment variable.

<?php

declare(strict_types=1);

namespace MyVendor\Sitepackage\Configuration;

use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;

class SiteConfiguration extends \TYPO3\CMS\Core\Configuration\SiteConfiguration
{
    public function __construct(string $configPath, PhpFrontend $coreCache = null)
    {
        parent::__construct($configPath, $coreCache);
        $this->cacheIdentifier = $this->cacheIdentifier . '-' . md5(getenv('TYPO3_BRAND_VARIANT'));
    }
}

Be careful though. The `SiteConfiguration` class is marked as internal and might change without notice.

Feel free to use the example. Comment and let me know, what you think.