Removing home page folder from URL

Hi,

What is the best approach to remove the home page folder from the URL?
The subpage template is article.php.

I experimented with adding site/models/article.php

<?php
class ArticlePage extends Page {
    public function url($options = null): string {
    return $this->uid();
   }
}

It removes the folder from the URL but drops a 404 error on click.
And I just couldn’t create routing in the config that works.

Tried this logic, but it is definitely not good

'routes' => [
        [
          'pattern' => 'home/(:any)',
          'action'  => function ($slug) {
            return go('/' . $slug);
           }
        ],
      ]

Routing beats me every time.

Thank you in advance.

You need two routes, basically like described here, but with the Kirby 3 way of registering the routes:

https://k2.getkirby.com/docs/developer-guide/advanced/routing#omitting-the-blog-folder-in-urls

1 Like

Thank you very much, Sonja, of course, its working now, as always when you give a solution. :sunglasses:

If someone else needs this in future, Page Model is higher in the thread, and routing that works is this:

'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);
          }
        ],
      ]

Just change home to your home page folder name, if you are using custom home page config.

Hello.

I’ve tried removing ‘home’ from url using route method.

I have two issues with this solution.

  1. Now in home page for each $page->children(): $item->url() still returns urls with /home .
  2. After navigating to other page there is a moment when you can actually see url changing in browser url bar. This doesn’t look pretty and doesn’t meet my project requirements.

Is there any solutions to these problems ?
Or perhaps there is another method how we can remove ‘home’ from url?

Thank you.

You need two routes and a model for the subpages with a modified url() method.

Route one listens to the new pattern and return the correct page.
Route two is a fallback that reroutes the old url to the new url (just in case)

The url method in the model returns the new url pattern.

Could you provide any reference in documentation or code sample?
Trying to figure out how that modification of url should look like. And if I need more routes for that, cause I’ve already used:

    [
  '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);
  }
],

Are you on a multi-lang site? Otherwise use return $page, without the site()->visit() part.

Apart fromt that, the routes are ok. So all that is missing is the page model.

1 Like

I would like to keep multi-lang as an option and using “return $page” seems like doesn’t change much.

I see how url’s can be modified. Thank you! I think now I’m on a right track.