More control over Routing


I want to apply something similar as the last example in the Routing doc page:

So I want:

to be:

I tried the example in the above page but I want to modify it so that:

  1. Trying to access “” returns a 404 instead of doing a 302 to
  2.” (without the trailing slash) should do a 301 redirect to “” (with the slash)

And I would want to do the same for a “pages” directory.

Can this be done with Kirby routes, or is it best to do it directly in the .htaccess?

I believe I found a solution using Kirby routing + htaccess redirect (for the trailing slash).

In config.php:

c::set('routes', array(
  'pattern' => '(blog(/.*)?|pages(/.*)?)',
  'action'  => function() {
     $page = site()->errorPage();
     return site()->visit($page);
  'pattern' => '(:any)',
  'action'  => function($uid) {

     $page = page($uid);

     if(!$page) $page = page('blog/' . $uid);
     if(!$page) $page = page('pages/' . $uid);
     if(!$page) $page = site()->errorPage();

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


In .htaccess (after the code for making panel links work)

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_URI} !(.*)/$
 RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
1 Like