Url class removes www

Hi folks,

by solving a redirection problem in my current project I noticed that the kirby url helper removes the www part. https://www.domain.de becomes https://domain.de.
(I have not yet tested if this happens also with other subdomains.)

Which leaded in my case to an unnessesary http redirect (the client wants the www in his domain) and the loss of get parameters (probably because of my poor non-www → www redirect statement in my .htaccess :wink:

Using $page->url() instead of Url::current() (or any other method of the url helper) solved my problem, because $page->url() does not alter the current url.
(But I find this hard to remember for future projects… )

So my question is: Is it behaviour wanted or might it be an issue of the kirby url helper?

I’m a bit confused. So do you mean the url() helper function (in /kirby/config/helpers.php, which internally uses the to() method of the Url class? Or are we talking about Url::current() and in which context are you actually using this method?

Also, how do you redirect?

Sorry, I was talking about the Url Class (Url | Kirby CMS), not the helper. This was misleading (i therefore changed the title of this thread accordingly).

I was using the Url class i.e. within a form action. After submitting the form therefore from https://www.domain.de/myform to https://domain.de/myform (because of the url class without www) a redirection rule in my .htaccess file was redirecting back to https://www.domain.de which prevented the form handling in my controller.

To me that sounds more like a configuration problem.

But you didn’t answer all my questions. So again, in which contect do you use Url::current()? And what does your .htaccess` redirect look like.

Also, which Kirby 3 version are you using?

I am using kirby 3.5.7.1

The redirect rule in my .htaccess looks like:

RewriteCond %{HTTP_HOST} ^client-domain\.de$ [NC]
RewriteRule ^(.*)$ https://www.client-domain.de/$1 [L,R=301]

I am sure this all could be improved…:wink:
But back to my question (Is the removing of www by the url class intentional?): Can you reproduce it (independent of the context - I am not sure if my context has anything to do with this?). Anyway if you need nevertheless more context please tell me.

No, the URL class does not remove subdomains. Where in the source code do you think does this happen?

You can for example test this with for example:

<?php echo Url::to('https://www.google.com'); ?>

As I said, this is most likely some sort of server side misconfiguration or some other issue with your code.

Do you have a link to the form where this happens?

I can confirm that on the client server
<?php echo Url::to('https://www.google.com'); ?>
echoes ‘https://www.google.com’.

But still, as I said before, on my clients domain (not sure if I should post the real domain?, let’s say ‘https://www.client-domain.com’)
<?php echo Url::current(); ?>
echoes ‘https://client-domain.com’ (without www),
<?php echo $page->url(); ?>
echoes ‘https://www.client-domain.com’ (with www).

Well, then thanks for your help. If you cannot reproduce it, it is most likely a server configuration issue (it’s on a mid-size shared webhostig package at IONOS).

Well, I can’t test it because I have no project running on www.

And you have not set the url config option by any chance? Or caching enabled?

After some additional tests I came to this conclusion:

On the hosting product of my client (IONOS Webhosting Premium from 2009) gets ‘www.’ stripped out of the php variable $_SERVER[‘SERVER_NAME’] (if your domain contains www.).

I guess (without further investigation) that therefore also some kirby functions return your domain without ‘www.’, i.e. Url::current() and $page->url().

So if you configured your hosting like www.your-domain.de (with www.) these vars/ functions will return your-domain.de (without www.).

Sidenote: In my previous posts I mentioned that $page->url() returns the domain with ‘www’ (in contrary to Url::current()). This only happened because I defined this in the kirby config, because without I could not access the panel on the IONOS webserver:
return [ 'url' => 'https://www.your-domain.de' ]
(thank you @texnixe for your last guess!)

In case you want to reproduce or run into similar problems, here is my setup:
I tested with a brandnew downloaded Kirby plainkit and different Kirby core versions (3.5.7.1 and 3.6.1.1) on three different Server environments (local MAMP, netcup and IONOS), without any forwardings/ rewrite rules or any other additional codes.
I just placed in the following code in the default template (/site/templates/default.php):

Url::current(): <?php echo Url::current(); ?><br> $page->url(): <?php echo $page->url(); ?><br> $_SERVER['SERVER_NAME']: <?php echo $_SERVER['SERVER_NAME']; ?><br> $_SERVER['HTTP_HOST']: <?php echo $_SERVER['HTTP_HOST']; ?><br>

Given that your domain is www.your-domain.de the result on the IONOS Webhosting is:

Url::current(): http://your-domain.de/
$page->url(): http://your-domain.de
$_SERVER['SERVER_NAME']: your-domain.de
$_SERVER['HTTP_HOST']: www.your-domain.de

In summary: It has nothing to do with kirby.

1 Like