Remove subdomain (www) from site url

I’d like to have the internal site url without the www subdomain which is added even though the browsers url is without it.

I have succeeded (see config.php source code below), but I am looking for a better way. Is there one?

<?php

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
$server_name = preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']);

return [
  'url' => $protocol . $server_name,
];

What do you mean, you are always redirected to www? Then there seems to be some redirect already in place somewhere.

To redirect from www to non-www, you should add the rewrite rules in your .htaccess (or the server config file). If there is already a redirect in place, make sure not to end up in an endless loop.

Sorry, I should have described it better. Inside my code I have various places where I use kirby functions like $site->homepage()->url(). These are returning the site url with www even when the site is called without it.

The code in my question solves this issue. But I don’t like the solution.

Thanks
René

That might be browser caching issue. And when you call the site without www, no rewrite to www happens?

Nor do I :wink: And I think it would make more sense to debug why this is happening rather then trying to solve this in the config.

Normally that should not happen, though and only source of truth should be available. So there should either be an automatic rewrite from www to non-www or vice versa. Then you wouldn’t run into this issue at all.

A new day and a fresh look. Today I can no longer understand the problem either. Everything seems to be correct, even without the change in the config. I probably just looked at it wrong.

We can delete my question, I’m sure it won’t help anyone, right? :slight_smile: