Change Context - Multidomain

Hi all,

i have a problem with changing the context of a multidomain system:

I got 3 contexts:

cultureKey: **web**
site_url: **https://www.example1.de**
error_page: **7**

cultureKey: **pm**
site_url: **https://www.example2.de/**
error_page: **50**

cultureKey: **sa**
site_url: **https://www.example3.de/**
error_page: **61**

My gateway Plugin:
<?php
if($modx->context->get(‘key’) != “mgr”){
//grab the current domain from the http_host option
switch ($modx->getOption(‘http_host’)) {
case ‘example1.de’:
case ‘www.example1.de’:
$modx->switchContext(‘web’);
$modx->setOption(‘cultureKey’, ‘web’);
break;
case ‘example2.de’:
case ‘www.example2.de’:
$modx->switchContext(‘pm’);
$modx->setOption(‘cultureKey’, ‘pm’);
break;
case ‘example3.de’:
case 'www.example3.de ':
$modx->switchContext(‘sa’);
$modx->setOption(‘cultureKey’, ‘sa’);
break;
}
}

My htaccess:

Options +FollowSymlinks
Options -MultiViews
RewriteEngine On
RewriteBase /

# Rewrite domain.com -> www.domain.com -- used with SEO Strict URLs plugin
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=permanent,L] .

# Prevent rewrite the .well-known directory used by LetsEncrypt by rules below of this rule
RewriteRule "^\.well-known/" - [L]
RewriteRule "/\.|^\.(?!well-known/)" - [F]

#Babel
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(sa|pm|web)/favicon.ico$ favicon.ico [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(sa|pm|web)/assets(.*)$ assets$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(sa|pm|web)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]

The page preview of example1.de and example2.de works without any problems. But as soon as I try to view any page from example3.de, I get the 404 page from the first context. Independent from the template.

My Hosting is all-inkl.com an all 3 domains root in the same folder with php 7.3.

MODx Version: 2.8.3

Any idea?

If you have different domains for your different contexts, then you don’t need all these rules:

RewriteRule ^(sa|pm|web)/favicon.ico$ favicon.ico [L,QSA]
...
RewriteRule ^(sa|pm|web)/assets(.*)$ assets$2 [L,QSA]
...
RewriteRule ^(sa|pm|web)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]

The friendly url rule from the default .htaccess-file should be sufficient.

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Also, in your gateway plugin the line

$modx->setOption('cultureKey', 'web');

doesn’t make much sense. The “cultureKey” should be a language code like “de” or “en” and is not the same as your context-key. If all your domains have the same language, then you don’t need this line.

1 Like

In your plugin code there is an unnecessary space character at the end of the string.

case 'www.example3.de ':

This could be the reason, why if fails for example3.de.

I would use the xRouting extra for this.

1 Like

I fixed the points and deleted htaccess entries and get the same problem.

@jako Now i installed XRouting, disabled gateway Plugin and configure my context like:

base_url: https://www.example2.de/
site_url: https://www.example2.de/
http_host: example2.de
cultureKey: pm
site_start: 8

Now i get the 404 error-site from “web” context if i try to see sites in “pm” context.

I believe the base_url should just be a slash /.

Also, I’m still not sure what the language “pm” is.

1 Like

Does the (anonymous)/Administrator user group has an access permission for the pm context?

(anonymous) should use minimum ‘Load Only’ (or ‘Context’) policy
Administrator should use ‘Administrator’ policy

1 Like

Reiterating what Halftrainedharry said: base_url should just be a /.
And I think you are confusing culture keys with contexts. You can remove the culture_key setting as it is incorrect and it doesn’t look like you’re creating a multilingual site. (If you are, then this should be set to an actual language code not the name of the context.)

You can add an error page for each context with the error_page setting. Just set to the id of each error page in the same way you have site_start.

As far as I can recall, when using XRouting, you cannot view unpublished pages on the additional contexts even when logged in as super admin. There is a workaround somewhere but I have never implemented it. So that may be why you are seeing the 404s.

2 Likes

Solved!

Thank you, the error was indeed in the base_url. I have set this as Halftrainedharry said to “/” and now the context can be called.

I had used the CultureKey for calling individual chunks, so for example [[$footer_[[++cultureKey]]]] - “footer_pm”. That’s why I had formulated that. But I’d rather use an [[*context-key]] for that.

1 Like

Here is the overview of my settings.

1.) XRouting Extra installed

2.) Create new context:

3.) .htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options -MultiViews
RewriteEngine On
RewriteBase /
</IfModule>

# Rewrite domain.com -> www.domain.com -- used with SEO Strict URLs plugin
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=permanent,L] .

# Prevent rewrite the .well-known directory used by LetsEncrypt by rules below of this rule
RewriteRule "^\.well-known/" - [L]
RewriteRule "/\.|^\.(?!well-known/)" - [F]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.