File URLs change when pages are moved or made invisible

No. As we wrote above, Kirby already supports file URLs without the sorting numbers. :slight_smile:

This doesn’t seem to work for multi language websites. It only works in the default language and throws 404 errors in the other languages.

Is this normal or am I doing something wrong?

If I look at $page->url() I think (sorry not tested yet) you can add in the first brackets something like $site->language() or $site->language()->code() (look at $site->language($code = null)).

But this only works on multi-language sites!

Good luck!

Added:
thanks to @texnixe

You can pass the language code to the url() method: https://getkirby.com/docs/cheatsheet/page/url

<?= $page->url($site->language()->code()) . '/' . $file->filename() ?>
1 Like

This still produces a 404 error. :confused:

What does the generated URL look like? Are you using URL keys?

@PaulMorel:

What’s, if you only add:

For both $page->url($site->language()->code()) and $page->url(), the URL is the same.

http://example.com/fr/premiere-page/deuxieme-page/document.pdf

In the english version, the link works.

http://example.com/en/first-page/second-page/document.pdf

I get an error:

Whoops \ Exception \ ErrorException (E_WARNING) Illegal offset type in isset or empty

I did a quick test and the URL that is generated by that code looks alright and the link works for me (tested in a 2.4.1 Langkit)

<?
$image = $page->images()->first() ?>
<a href="<?= $page->url($site->language()->code()) . '/' . $image->filename() ?>">link to image</a>

Edit: $site->language() will not work.

1 Like

May be

is your default language, which is omitted in the path by default?

Then I would try to omit it here too…

The resulting HTML, without lang code in the default language

<a href="http://localhost/alang/de/projects/project-a/closeup.jpg">link to image</a>
<a href="http://localhost/alang/projects/project-a/closeup.jpg">link to image</a>

Which Kirby version are you using and what does your language setup look like in config.php?

1 Like

@anon77445132: en is my default, but my config might be wrong.

@texnixe: Right now I’m on 2.4.0. I’ll update to 2.4.1 tomorrow see if it makes a difference. Here’s my language config:

c::set('languages', array(
  array(
    'code'    => 'en',
    'name'    => 'English',
    'default' => true,
    'locale'  => 'en_CA',
    'url'     => '/en',
  ),
  array(
    'code'    => 'fr',
    'name'    => 'Français',
    'locale'  => 'fr_CA',
    'url'     => '/fr',
  ),
));
1 Like

@PaulMorel:

It should look like

<a href="http://example.com/fr/premiere-page/deuxieme-page/document.pdf">link to image - fr</a>
<a href="http://example.com/en/first-page/second-page/document.pdf">link to image - en</a>

What happens, if you you add something like my above code as a static text (with correct links) in a textarea, which is renderd with kirbytext( ... )?

Added:

<?php if($site->multilang()): ?>
This is a multi-language website
<?php endif ?>

works correct?

Since you are using the language code for both languages, the urls should have that as well, so your links are in fact correct.

Edit: But the error occurs once I add an URL-Key, so that does look more like a bug.

The interesting thing is that without URL-Key, the correct link with lang code reroutes to an image URL without the lang code.

Edit: I created an issue on GitHub.

2 Likes

Yeah, that works. Hardcoding the links in the text area does the same. I get a 404 on the french link.

Hmm, I should’ve specified that I was using URL-Key for the french language.

At least now know that it’s a bug.

1 Like

@PaulMorel:
What is the working link to the file with the numbers from the visible pages sort for the french version of that webpage?

I don’t know about the translated directory names in such a link…

Added:
I want to know your normal link with the sort numbers of the directories to think of a solution…

What’s about to use only

for BOTH languages, if you link to the same file?

Added:
This may be a work around until the Kirby code get’s corrected…

If we use a standard image link, neither the lang code nor the URL-Keys are used:

<a href="http://localhost/alang/content/1-projects/1-project-a/closeup.jpg">link to image</a>

The link is always the same, no matter if default language or not.

I think, we want NOT to use the sort numbers…

This should work:

<? $image = $page->images()->first() ?>
<a href="<?= $site->url() . DS . $page->id() . DS . $image->filename() ?>">link to image</a>

It creates the link without language code and without the URL key, in the same way the standard, numbered image link is created, only without the content folder and without the numbers.