For coders TYPO3 Tech Corner

Use different domains within the same site in TYPO3

Use different domains within the same site in TYPO3

I have often read the answer on various sources that you don't need several domains for one area in TYPO3. Finally, you can use an environment variable to assign different domains, for example if you want to develop locally or have a test environment.

Environment Variables

This is relatively easy to do by picking up this variable in the SiteConfiguration:

base: 'https://%env(HOST_MAIN)%/' rootPageId: 1 websiteTitle: 'My website' imports: - resource: 'EXT:sitepackage/Configuration/Routes/DefaultSiteConfiguration.yaml'

In the Apache configuration, this variable can then be easily set with:


Alternatively, this can also be easily defined in an .env file (here we recommend using helhum/dotenv-connector):
BaseVariants with own Condition

But there can also be cases in which you have to allow multiple domains for the same page tree section within the same TYPO3 instance (e.g. Production). This applies, for example, to areas in the intranet. In another case, however, an insurance customer would like us to access one area via two different domains. Depending on the domain, the content is then slightly different (we do not show the implementation here in order not to complicate the example).

This can be made possible with the help of BaseVariants in the SiteConfiguration. The only challenge: A condition that switches based on the current domain does not yet exist in TYPO3. We have to help a little here.

Let us assume that we have now defined two different domains in the .env file:

Then the SiteConfiguration could look like this:

base: 'https://%env(HOST_MAIN)%/' baseVariants: - base: 'https://%env(HOST_ALTERNATIVE)%/' condition: 'isAlternativeDomain("%env(HOST_ALTERNATIVE)%")' rootPageId: 1 websiteTitle: 'My website' imports: - resource: 'EXT:sitepackage/Configuration/Routes/DefaultSiteConfiguration.yaml'

The problem, however, is that the isAlternativeDomain() condition does not yet exist, and we have to make up for that in our sitepackage, for example. EXT:sitepackage/Configuration/ExpressionLanguage.php:

<?php return [ 'site' => [ \Vendor\Sitepackage\Condition\SiteConditionProvider::class ] ];

The related provider EXT:sitepackage/Classes/Condition/SiteConditionProvider.php:

<?php declare(strict_types = 1); namespace Vendor\Sitepackage\Condition; use Vendor\Sitepackage\Condition\FunctionsProvider\SiteConditionFunctionsProvider; use TYPO3\CMS\Core\ExpressionLanguage\AbstractProvider; /** * SiteConditionProvider */ class SiteConditionProvider extends AbstractProvider { /** * Constructor */ public function __construct() { $this->expressionLanguageProviders = [ SiteConditionFunctionsProvider::class, ]; } }

Last but not least the FunctionsProvider in EXT:sitepackage/Classes/Condition/FunctionsProvider/SiteConditionFunctionsProvider.php:

<?php declare(strict_types=1); namespace Vendor\Sitepackage\Condition\FunctionsProvider; use Symfony\Component\ExpressionLanguage\ExpressionFunction; use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * SiteConditionFunctionsProvider */ class SiteConditionFunctionsProvider implements ExpressionFunctionProviderInterface { /** * @return array|ExpressionFunction[] */ public function getFunctions() { return [ $this->isAlternativeDomain(), ]; } /** * Site configuration: Can be used to decide if a baseVariant should be loaded * * Example: * baseVariants: * - base: 'https://%env(HOST_MAKLERNETZ)%/' * condition: 'isAlternativeDomain("%env(HOST_MAKLERNETZ)%")' * * @return ExpressionFunction */ protected function isAlternativeDomain(): ExpressionFunction { return new ExpressionFunction('isAlternativeDomain', function () { // Not implemented, we only use the evaluator }, function (array $existingVariables, string $alternativeDomain) { return GeneralUtility::getIndpEnv('HTTP_HOST') == $alternativeDomain; }); } }

In principle, that's all. The same area in the page tree can now be accessed via the domains and


"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us