Simple routing for multilanguage website

Hello everyone,

for some reason my brain refuses to figure out how routes work in Kirby 3.

For example i´ve got a multi-language website with the following url:
mywebsite.de/de/meta/imprint

And i want it to route to a shorter url:
mywebsite.de/de/imprint

This is what i have so far:

      'routes' => [
    [
      'pattern' => '(:any)',
      'language' => 'de',
      'action' => function ($uid) {
        
        $page = page($uid);
        
        if(!$page) $page = page('meta/' . $uid);
        if(!$page) $page = site()->errorPage();
        
        return $page;
        
      }
    ],
    [
      'pattern' => 'meta/(:any)',
      'language' => 'de',
      'action'  => function($uid) {
          go($uid);
      }
    ]
  ]

I tried a lot of other configurations but nothing worked so far. Help is much appreciated!Preformatted text

Your routes should look like this:

    'routes' => [
        [
          'pattern' => '(:any)',
          'language' => 'de',
          'action' => function ($language, $uid) {
            
            $page = page($uid);
            
            if(!$page) $page = page('meta/' . $uid);
            if(!$page) $page = site()->errorPage();
            
            return site()->visit($page, $language);
            
          }
        ],
        [
          'pattern' => 'meta/(:any)',
          'language' => 'de',
          'action'  => function($language, $uid) {
              go($language. '/' . $uid);
          }
        ]
    ]

The first param of the anonymous function is the language

1 Like

Works perfectly, thank you very much!!

Another question: What do i do to set this route for every language of my website (could be a lot of languages).

Should be possible by setting the language option to *

You just made my life simpler, it works! Thank you!