Change home page (index) from the panel?

Hi,

Let’s say I have 4 pages (default template) and one blog page (blog template). Now I want to make the blog the index (home page) of my site. Is it possible to make this change? Even if I remove the current home page from the content folder, I can’t change the url of the blog page to “/”. Suggestions?

You can set your new home page in the config: https://getkirby.com/docs/reference/system/options/home

If you want to do this from the Panel, you have to wrap this option within the ready setting to set the home page according to a site field selected in the Panel (since you can’t use kirby() and site() outside of the ready option in the config)

<?php

return [
    // other settings…
    'ready' => function() {
        return [
            'home' => ($p = site()->setHomePage()->toPage()) ? $p->id() : 'home'
        ];
    }
];

(Assuming you have a setHomePage field of type pages in site.yml…)

Thanks, I added the following to my site.yml:

setHomePage:
  label: Set home page
  max: 1
  type: pages

And your code to my config but not doing anything :thinking:

Hm, maybe the option kicks in too late, just tested myself, and. when I dump $kirby->options() in a template it outputs the right option setting, but you are right, it is ignored…

Hm, time to sleep now…

Yes, not really urgent. Really appreciate the help, need to sleep here as well :grimacing: I’m blaming Kirby for not getting enough sleep.

Ok, even if an option from ready kicks in too late, we can still solve this with a route:

'routes' => [
    [
        'pattern' => '/',
        'action'  => function () {
         
          if ( $page = site()->setHomePage()->toPage() ) {
            return $page;
          }
          $this->next();
        }
    ]
],

With this route we either return the page set in the setHomePage field if it exists, otherwise we fall back to the default behavior.

This works. The only issue I’m having is if I set the blog as my homepage, the code doesn’t redirect example.com/blog to example.com thus having two urls to the same page.

This makes it just a little inconsistent. If for example you have (my header):

Logo--------------------------------------Blog—About—Contact

-> If I click on the “Logo” it will take me to the blog example.com
-> If I click on the “Blog” link it will also take me to the blog but example.com/blog

You wouldn’t have the issue if you just set the home page to blog in the config, without making it configurable through the Panel. Why is this necessary? I don’t think it makes sense to change this ever so often.

Ok here’s my issue:
If my main page is my blog:
1_home (title of the page is Blog)
2_about
…

Now all my blog posts will have the following urls:
example.com/home/my-blog-post

What’s the easiest way to change the slug from the panel? :thinking: I was thinking to have an input field in the settings where you can change the slug of the home page but not sure.

I know you can add 'home' => 'blog' to config.php but then you also need to rename the folder to blog.

What’s the problem with changing the folder name to blog? Why does it have to be configurable?

Looks like we are now discussing this in two places, which doesn’t really make sense. So I’ll move these posts.

Ok this works:

'routes' => [
  [
    'pattern' => '/',
    'action'  => function () {
      if ($page = site()->setHomePage()->toPage()) {
        return $page;
      }
      $this->next();
    }
  ],
  [
    'pattern' => '(:any)',
    'action'  => function ($uid) {
      $page = page($uid);
      if ($page === site()->setHomePage()->toPage()) {
        return go(site()->url());
      }
      return $page;
    }
  ]
],

Not sure if I should do the 301 redirect though return go($page->link(), 301);

Ok tested this on a multi-lang setup
giphy