Remove /blog/ from the URL

Hi folks,

I’ve set the /blog page as my homepage on my site with this:

return [
    'home' => 'blog',

It works well, but blog post URLs are bit funky, in that they now include /blog/ in the URL. I’d like to remove this, which I’ve been able to do with the following route:

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

However, on my blog home page (where all my blog posts are listed), the links to the single posts still include the /blog/ part of the URL.

How do I remove them from there too? Basically, I’d like /blog/ to go away from my site entirely, as my homepage is my blog, so the concept of /blog/ just isn’t a thing on my site.

Thanks!

Ideally, you overwrite the url() method in your article page model to output the correct URL and prevent the redirect from the old to the new URL.

Example here: Overwrite url() in page model

Adapt to what you need to output.

That worked perfectly, thank you. For anyone who’s interested, here’s the model I made as /site/models/article.php:

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