Omit a variable letter or number in route

In my site, I have a basic ‘profiles’ section with profile pages, where there’s a subfolder for each first letter of the users, because else there will be +/- 900 pages in the /profile/ folder. The structure:

   mysite.com/social/profile/h/hank

where /h/ is the first letter of the username, and this folder contains all profiles with ‘h’ as first letter of the username.

I would like to visit pages on the front-end like this:

    mysite.com/social/profile/hank

So omitting the /h/ (or any letter or number) in the URL and still visit the right page :slight_smile: . There’s a guide (https://getkirby.com/docs/developer-guide/advanced/routing) on how to omit the ‘blog’ from a URL with two routes, but how can I do this with a variable folder name (will be 1 letter or number: a-z and 0-9) and still let all other pages work like normal?

That would be something like this:

c::set('routes', array(
  array(
    'pattern' => 'social/profile/[a-z0-9]/(:any)',
    'action'  => function($username) {
      go('social/profile/' . $username);
    }
  ),
  array(
    'pattern' => 'social/profile/(:any)',
    'action'  => function($username) {
      $page = page($username);

      if(!$page) $page = page('social/profile/' . str::substr($username, 0, 1) . '/' . $username);
      if(!$page) $page = site()->errorPage();

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

Woohoo :slight_smile: That works! The str::substr($username, 0, 1) part is a clever solution that I didn’t thought of yet! Thanks for your help!