Routing: Omitting everything but uid


#1

Is it possible to achieve the following:

site.com/folder-1/subfolder-2 -> site.com/subfolder-2

site.com/folder-3/subfolder-1 -> site.com/subfolder-1

site.com/folder-2/subfolder-3/subsubfolder -> site.com/subsubfolder

… and so on.
Subfolder uids are all unique.

Thanks!


#2

You can achieve this with custom routes. Take a look here, especially at the very end (“Omitting the blog folder in URLs”): https://getkirby.com/docs/advanced/routing


#3

I’m trying to implement a similar feature, but not throughout the whole site, only on a specific folder.

The folder structure is:

Magazine

    • issue-1
    • issue-2
    • …etc
    • issue-18
      • article

And I want all article urls to be ‘domain.com/article’ instead of ‘domain.com/magazine/issue-n/article’.

Have been reading about routes and lots of helpful posts on here, but am missing something, as I have got everything directing to ‘domain.com/article’ but it’s showing the error page.

Here’s my latest attempt, any pointers would be much appreciated!

c::set('routes', array(
  array(
    'pattern' => 'magazine/(:any)',
    'action'  => function($uid) {

      $page = page($uid);

      if(!$page) $page = page('magazine/issue-' . str::substr($uid, 5) . '/' . $uid);
      if(!$page) $page = site()->errorPage();

      return site()->visit($page);

    }
  ),
  array(
     'pattern' => 'magazine/(:any)/(:any)',
     'action'  => function($issue, $article) {
       return go($article);
     }
  )
));

Edit: The page ‘domain.com/magazine’ must still work, and would it be possible to have ‘domain.com/magazine/issue-n’ also still work?


#4

The first route is not correct, try this (untested)

c::set('routes', array(
  array(
    'pattern' => '(:any)',
    'action'  => function($uid) {

      $page = page($uid);

      if(!$page) $page = page('magazine')->grandchildren()->findby('uid', $uid);
      if(!$page) $page = site()->errorPage();

      return site()->visit($page);

    }
  ),
  array(
     'pattern' => 'magazine/(:any)/(:any)',
     'action'  => function($issue, $article) {
       return go($article);
     }
  )
));

#5

Ah, I understand! This works, thank you :grin: