Blog posts without /home in the path

I would like a clean and simple address for my blog posts without a folder structure.

Currently I have:
domain.tdl/home/my-blog-post

I would prefer:
domain.tdl/my-blog-post

How do I set up the blueprints / templates to avoid having ‘home’ show as a directory in the web address?

2 options:

  • Your blog posts live directly in the content folder instead of having them as children of home
  • You redirect the posts via a combination of two routes: Routes | Kirby CMS.
    Additionally (and optionally), you can create a page model for the blog posts where you overwrite the url method to reflect the intended url structure: Page models | Kirby CMS
1 Like

Close, but something is not quite right. The address part is working (domain.tdl/my-blog-post) but the request is being redirected to the error page. I’m using the page models method?

/site/models/article.php

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

/site/templates/home.php

<?php foreach ($page->children()->listed()->sortBy('date', 'desc') as $article): ?>
	<a href="<?= $article->url() ?>"><?= $article->title() ?></a>
<?php endforeach ?>

This only works in conjunction with the routes, simply changing the url will indeed go to the error page.

Ah, apologies… now I understand, Its working now :slight_smile:

Can I use the same method with a tag in the URL?

For example:
domain.tdl/journal lists articles with the tag ‘journal’

A little stuck rendering posts with a tag of ‘journal’ using home:

Address: /home/post-1
Rewriten to: /post-1
Template: article.php

When I click on a tag, say ‘journal’ and render an address of /journal I get an error page (/site/template/default.php) instead of the home page.

I’ve added a route for journal but obviously going wrong somewhere:

/site/config/config.php

<?php

return [
    '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' => 'journal',
            'action' => function ($tag) {
                return page('home')->render([
                    'tag' => $tag
                ]);
            }
        ]
        
    ]
];

/site/models/article.php

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

Well, the journal route would have to come before (:any), because (:any) already catches it, so no need for Kirby to try the next route, unless you specifically call the next route if you encounter an error in the first.