Rerouting blog in subfolder

Just trying Kirby for the first time on a project. Nearly finished but want to tide up the urls and understand how to use the router for desired effect, can’t seem to get it working with examples from the docs

I have a blog folder that’s within a subdirectory, current url
example.dev/about-us/blog/article

desired url
example.dev/blog/article

same with the blog archive, wanting to change the url from
example.dev/about-us/blog to example.dev/blog

Grateful if anyone can anyone help me.

Why not move the blog folder to /content/ instead of /content/about-us/ ?

While this is possible with the router, I agree with @bvdputte, unless you have a very good reason to have the blog as a subfolder of the about page, I would simply move it one up.

Thanks for the suggestions. I was thinking along the same lines however the clients want the blog within a submenu in the main site menu.

So if I move the blog to the main directory which solves the url problem, what’s the best way of linking to it within a submenu?

Well, imo, if the client wants it in a submenu, then it would sound logical to me to have that reflected in the URL.

But, you can always add the the blog to the submenu via an if-statement — that checks if you’re in the correct submenu item — in the menu snippet.

If you prefer to go the routes way, this should work:

c::set('routes', array(
   array(
      'pattern' => 'about-us/(:all)',
      'action'  => function($uid) {
         go($uid);
      }
   ),
   array(
      'pattern' => '(:any)(:all)',
      'action'  => function($u1,$u2) {
         $page = page($u1.$u2);

         if(!$page) $page = page('about-us/' . $u1.$u2);
         if(!$page) $page = site()->errorPage();

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

Here’s my current workaround that seems to working. Feels a bit hacky and there may be a better way but thought I’d add it here if it’s helpful to anyone else.

Moved the main blog/ folder to the content/ folder. Made it hidden so as to not show in the menu.

Created a dummy blog/ folder inside the parent about-us/ and added file redirect.txt to the dummy blog folder inside of which is simply

Title: Blog
----
Redirect: ./blog

Then in the a menu snippet, the logic to create a submenu :

<?
      $children = $item->children()->visible();
      if ($children->count() > 0) : ?>
        <ul class="sub-menu">
          <? foreach($children as $child):
                $link = $child->redirect()->exists() ? $child->redirect() : $child->url(); ?>
                <li><a href="<?= $link ?>"><?= $child->title() ?></a></li>
          <? endforeach ?>
        </ul>
      <? endif ?>

The one good thing about this is that the client doesn’t have to navigate through the parent folder in the kirby panel to add blog articles in future.

Definitely the easiest way to go about this. I’d then hide that page in the Panel, so as not to confuse users with this duplicate blog page.

And also, make sure that this page is not directly accessible via its URL (use a route or a redirect.php template that redirect this page to the real blog page in case someone tries to call it manually).