Omit the blog folder in the URL

Turns out there was just a small issue with the code. This code works for me (I also made some further improvements):

$visitRoute = function($uid) {

  $site = site();
  $lang = kirby()->route->lang;
  $page = $site->visit($uid, $lang);

  if($page === $site->errorPage()) $page = $site->visit('blog/' . $uid, $lang);
  if($page === $site->errorPage()) $page = $site->visit('projects/' . $uid, $lang);

  return $page;

};

c::set('routes', array(
  array(
    'pattern' => '(?:blog|projects)/(:any)',
    'action'  => function($uid) {
      go($uid);
    }
  ),
  array(
    'pattern' => '(?:de/blog|de/projects)/(:any)',
    'action'  => function($uid) {
      go('de/' . $uid);
    }
  ),
  array(
    'pattern' => 'de/(:any)',
    'action'  => $visitRoute,
    'lang'    => 'de'
  ),
  array(
    'pattern' => '(:any)',
    'action'  => $visitRoute,
    'lang'    => 'en'
  )
));

Of course you will need to adapt the code if your projects or blog page has a URL key in German. It works with URL keys for the individual children, but not for the parent currently.

2 Likes