Domain specific config not working

I am wondering how Kirby determines the domain name the site is running on. I have one site where the config.php always gets used despite a specific config for the domain existing.

The only thing different with this particular site is that there is a .htaccess rule that forces the www at the start of the URL. Could this be interfering?

The config file name is in the form of config.domainname.com.php. Do I really need to put the www in the file name?

Yes, of course. The www.example.com domain is a different domain from dev.example.com or just example.com.

I see. I think the docs could be a little clearer to point that out :slight_smile: Currently the help page on this assumes there are no www in the domain.

Hm, the examples don’t have a www subdomain, but /site/config/config.staging.yourdomain.com.php. Were you assuming that www is another sort of subdomain than staging? But I can of course add it as another example.

I figured that Kirby would know that www.example.com and example.com is the same site but dev.example.com is a different place.

Well, while it is pretty unlikely that http://www.example.com and http://example.com are used for different purposes, from a technical point of view that would be possible. So it would be rather un-Kirby-ish to assume both are the same.

I ran into a similar issue with a custom site.php and a multiple domain setup lately.

Kirby uses server::get() when loading the config and inside this method the result is sanitized and this removes the leading www from the HTTP_HOST.

@flokosiol If that was the case, the domain specific config file would be loaded instead of the subdomain specific www file? I can’t see where this would remove the www from the SERVER_NAME?

$servername = 'www.example.com';
$value = preg_replace('![^\w.:-]+!iu', '', $servername);
dump($value);

// result: www.example.com

This:

$configs = array(
     'main' => 'config.php',
     'host' => 'config.' . server::get('SERVER_NAME') . '.php',
     'addr' => 'config.' . server::get('SERVER_ADDR') . '.php',
   );

on my domain www.example.com results in

Array
(
    [main] => config.php
    [host] => config.www.example.com.php
    [addr] => config.::1.php
)

Strange … I double checked this on a live site and the www gets removed when I var_dump the $configs … but locally it’s like you said!? Everything as expected …

@flokosiol I just tested this on a remote domain, just to make sure, with the same result as above.

And, as I wrote above, the preg_replace() function doesn’t remove the www bit, all it does is remove all characters that are not a word character (alphanumerical characters plus underscore including unicode characters), not a colon, not a dot, and not a dash. Since www doesn’t fall into any of these categories, it is not removed, as you can see from the example I posted above.

I don’t know what is happening on your site, but there’s nothing in the code to confirm your theory, I’m afraid?

I follow @texnixe, she is correct here as always :slightly_smiling: :sweat_smile: !

@jimbobrjames:
But your problem is, I think, outside of Kirby:

Put the following code temporarily (security !!!) in a template or snippet and call a page using that code:

<?php phpinfo(); ?>

In that page search for “SERVER_NAME” in the first column of a table.
And view that page by changing between your websites "www.example.com" and “example.com”.

Does the content in the second column change?

I guess no. Then this is your problem, which you may want to resolve. Look at your webservers configuration…

I don’t think there is any problem.

If you use the www.example.com domain, name your domain specific config config.www.example.com.php
If you use the example.com domain, name your domain specific config config.example.com.php.

If you reroute from non-www to www, you use the www domain, not the non-www domain.

@anon77445132 you won’t even see the phpinfo for the non-www domain because the rerouting takes place before PHP kicks in (unless you remove the redirect from the .htaccess but that changes everything, anyway).

Sorry to dig this one up again, but i’ve run into it again, on a different server. It only loads config.php, not the domain specific config.

If i put this in a comment tag in my site head:

<?php echo server::get(); ?>

The result is:

<!-- Array -->

Gives me nothing. How can it be that Kirby can’t figure out what the domain is, in order to load the config? Is there something else I can try to figure out whats going on?

Edit: Ah. It’s an array. Should have done…

<!-- <?php var_dump(server::get()); ?> -->

The info it gives me tells me the config is named correctly. The domain name has a hyphen in the middle of it. Would that be a problem?

Please ignore this. It suddenly started working, although i did nothing at all to correct it. Not sure what was going on there.