Routing to hide /home folder in URL killed draft review

I have a router setup to remove the /home/ from my primary blog post URLs.

It works great, but when viewing drafts, I’m always redirected to my error page. The draft URL with the temporary token includes /home/ in the URL, and I’m assuming that’s part of the problem, but I can’t seem to find my way around the issue.

This is my config.php file:

return [
    'debug'  => false,
    'routes' => [
      [
        'pattern' => '(:any)',
        'action' => function ($uid) {
      
          $page = page($uid);
      
          if (!$page) $page = page('home/' . $uid);
          if (!$page) $page = site()->errorPage();
      
          return site()->visit($page);
        }
      ],
      [
        'pattern' => 'home/(:any)',
        'action'  => function ($uid) {
          go($uid);
        }
      ]
    ],
 ];

Hm, don’t really know what is the best way. This will work to also find a draft, but also return the draft when the user is not logged in. So you would need an additional check if the user is logged in or not and if the page is a draft.

    'routes' => [
        [
          'pattern' => '(:any)',
          'action' => function ($uid) {
              if ($page = kirby()->page('home/' . $uid)) {
                  return $page;
              };
        
              $this->next();
          }
        ],
        [
          'pattern' => 'home/(:any)',
          'action'  => function ($uid) {
              go($uid);
          }
        ]
    ],

But maybe there is a better way.

1 Like

Honestly, this is perfect for this particular project. If someone wants to dig around and find drafts on my personal blog, more power to them.

Maybe at some point, I’ll transition to a proper homepage and move my “Updates” somewhere else like a normal human. Thanks so much @texnixe! :heart: