Router Regex for languages or empty language


i want to achive this regex pattern in my router:

So far i’ve got:

'pattern' =>  '^$|(de|fr|cn|pl|es|nl|it|fi|ru)/faq/(:all)' // codes are auto-generated from language-array.

Is there anything i am missing because the regular expression seems to be working fine?

Background Info:
The default Language is en and it’s not shown in the URL.


I’ve figured out a way to solve my problem, but it seems to be very ugly.
For now i set two different patterns:

# handle languages
'pattern' => '(:all)/faq/(:all)',
'action'  => function($lang, $path) {
    faq_router_handler($lang, $path);
# handle default language (which might fail for some rare urls?)
'pattern' => 'faq/(:all)',
'action'  => function($path) {
    faq_router_handler('', $path);

It just dont feel right.

I think you can use (:all?) to make it optional, which should solve your “unprefixed default language”-case. Or what are you trying to achieve?

Like so:

# FAQ router
'pattern' => '(:all?)/faq/(:all)',
'action'  => function($lang, $path) {
    faq_router_handler($lang, $path);

Unfortunately the (:all?)-Selector doesnt work at all :frowning:
Due to :all, :any… are special kirby selectors they don’t seem work with regular expressions together.

You can try something like this.
But havn’t tested this one specifically.

$kirby->set('route', [
  'pattern' => '(?:(^[a-z]{2})//?)?faq/(:all)',
  'method'  => 'ALL',
  'action'  => function($lang, $path) {
    faq_router_handler($lang, $path);

    // or if you need to set a default language
    faq_router_handler($lang ?? 'en', $path);

That’s bizarre. I wrote the optional (:all?) down in my notes at the Kirby workshop as a solution @bastianallgeier mentioned to fix my duplicate routes issue I had.

I don’t have the time to look into this right now, but indeed it doesn’t seem to work as expected. :confused: