Url not from media folder?

Hi, when I create a link to a file it appears like: www.page.com/subpage/121243434534673/file.pdf
Is it possible to change this path, or that it’s accessible from a path like this: www.page.com/subpage/file.pdf? (the file is stored in the “subpage” folder originally)

Thanks a lot in advance!

Hey @Antumbra, welcome to the forum.

Yes, that is possible via a custom file::url component:

Something like this will do it:

'components' => [
    'file::url' => function (Kirby $kirby, $file) {
      $page = $file->parent();
      if ($file->extension() == 'pdf') {
        return $kirby->url() . '/content/' . $page->diruri() . '/' . $file->filename();
      } else {
        return $file->mediaurl();
      }
    }
]

Will serve PDF files directly from the content folder, whilst allowing normal behaviour for other file types. Tweak the condition accordingly.

Thank you very much, I’ve added it like this, combining it from @texnixe 's link:

Kirby::plugin('yourname/old-file-urls', [
'components' => [
'file::url' => function (Kirby $kirby, $file) {
$page = $file->parent();
if ($file->extension() == 'pdf') {
return $kirby->url() . '/content/' . $page->diruri() . '/' . $file->filename();
} else {
return $file->mediaurl();
}}]]);

but now the problem is that if I reach a file with $page->myFile()->toFile()->url() is that it returns the link like this: http://www.page.com/1_subpage/file.pdf
note the folder number there at subpage. Is it possible to do it without that?

If you don’t want the folder number, you can adapt the URL so that it points to the subpage, then use a route that returns the correct file from the content folder.