More control over Routing

Hello,

I want to apply something similar as the last example in the Routing doc page:
http://getkirby.com/docs/advanced/routing

So I want:
example.com/blog/my-post/

to be:
example.com/my-post/

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

  1. Trying to access “example.com/blog/my-post/” returns a 404 instead of doing a 302 to example.com/my-post/
  2. example.com/my-post” (without the trailing slash) should do a 301 redirect to “example.com/my-post/” (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(
 array(
  'pattern' => '(blog(/.*)?|pages(/.*)?)',
  'action'  => function() {
     $page = site()->errorPage();
     return site()->visit($page);
  }
 ),
 array(
  '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