kirby routing 

So a thing about this site that I like and don't like is that the URLs have /in/ in them, as a way of organizing the years of posts. For example /in/2016/setup — and that's fine and I like it and I don't really want a subdirectory for each year at the root of the content directory. Fine. But it would still be nice to be able to type in "coordinate.systems/2015" without having to type the /in part. After having it on my I've had it on my to-do list since last June to read the Kirby docs and set up some custom routing to make /in smartly optional.

Finally able to check that off my list. Only took an hour or two, but that was an hour that just never floated to the surface.

Here's the custom routing for this site. (The commented lines would go the other way, removing '/in/ instead of adding it.)

/*

 ---------------------------------------
Custom Routes
 ---------------------------------------

*/

c::set('routes', array(
  array(
    'pattern' => 'feed.xml',
    'action'  => function() {
      // Use the feed page for the URI /feed.xml
      return page('feed');
    }
  ),
  array(
    'pattern' => 'feed',
    'action'  => function() {
      // Redirect /feed to /feed.xml
      return go('feed.xml');
    }
  ),
  array(
    'pattern' => 'sitemap.xml',
    'action'  => function() {
      return site()->visit('sitemap');
    }
  ),
  array(
    'pattern' => 'sitemap',
    'action'  => function() {
      return go('sitemap.xml');
    }
  ),
  array(
    'pattern' => 'in',
    'action'  => function() {
      header::redirect('/', 301);
    }
  ),
  // array(
  //   'pattern' => 'in/(:all)',
  //   'action'  => function($query) {
  //     header::redirect('/'.$query, 301);
  //   }
  // ),
  array(
    'pattern' => '(:num)/(:all)',
    'action'  => function($year, $query) {
      $inpage = '/in/' . $year . '/' .$query;
      header::redirect($inpage, 301);
      // return site()->visit($inpage);
    }
  ),
  array(
    'pattern' => '(:num)',
    'action'  => function($year) {
      $inpage = '/in/' . $year;
      header::redirect($inpage, 301);
      // return site()->visit($inpage);
    }
  ),
));

as a gist