Replicate URL with router - some questions

I’ve been trying to replicate an existing URL scheme used by a website in a new Kirby-powered one. The website featured a simple blog structure (eg. www.domain.com/blog/this-is-an-article). The new website add dividers in the URL to clean things up. A separate page (tips-and-tricks) containing articles results in the following URL => www.domain.com/blog/tips-and-tricks/this-is-an-article.

As you’ve might have guessed, when a user visits the original URL, they should be redirected to the article with the seperation.

I’ve been fiddling with the router to get this to work. The problem mainly is that there are more than one separation, which makes it harder to figure.

array( 'pattern' => array('blog/stories/(:any)', 'blog/articles/(:any)', 'blog/tricks/(:any)'), 'action' => function($blog, $sub, $uid) { $page = page('blog/' . $uid); go($page ? $page->url() : 'error'); } )

Any help? Extensive documentation on the helpdesk does not really help unfortunately.

Here you go:

c::set('routes', array(
  array(
    'pattern' => 'blog/(:any)',
    'action' => function ($uid) {
      if($page = page('blog')->grandChildren()->findBy('uid', $uid)) {
        go($page, 301);
      } else {
        return site()->visit('error');
      }

    }
  )
));
2 Likes

Genius. Works perfectly. Thanks a lot!