404 error on page

Hi everyone,

I just came up with a thought about 404 errors. It’s suggested in the One-pager solution that we redirect the generated subpages to the error page. But a best practice would suggest to display the error at that precise URL, with no redirection.

I think Kirby should have a function that should be called when undesired URL get accessed to force display the error template and exit what’s next:

<?php if (!$page->isHomePage()) error(); ?>

EDIT: exactly like it currently does for URLs that doesn’t match any page of the content/ folder.

This page would automatically set the HTTP response code as 404.
And a plus would be to be able to give the error code as an argument (404 as default) :

<?php if (!$page->isHomePage()) error(403); ?>

What do you think about it?

<?php if (!$page->isHomePage()) go('error') ?>

?

You can display a page without redirecting to a different URL using a custom router and calling the visit() method on the $site object.

c::set('routes', array(
  array(
    'pattern' => '(:all)',
    'action'  => function() {
      if(!page()->isHomepage()) {
        site()->visit('error')
      }
    }
  )
));
2 Likes

Thanks for the tip! I couldn’t make it work but that must be me.
Btw this should i.m.h.o. be the default behaviour :).

You need to put this into your config.php, not the template, to make it work.

Yep, still not work :confused: .