Totally no parent slugs in all pages urls

Hey Kirby Folks,

I’m totally bad in writing new routes for kirby and have the following question:

What is the easiest and bulletproof way to write routes, that eliminate all slugs of all pages urls.

For example no “https://www.mysite.com/blog/posting-a” and no “https://www.mysite.com/about-us/contacts” just “https://www.mysite.com/posting-a” and “https://www.mysite.com/contacts

Thanks for your help,
Christian

You can use the (:any) placeholder to catch all routes after the domain and until the next slash.

Then try to find a page with that slug.

Note that if you happen to have duplicate slugs under different parent pages, such pages wouldn’t be accessible anymore or rather, your route than would only deliver the first match, e.g.

mydomain.com/blog/example-slug and
mydomain.com/otherparent/example-slug

However, this shouldn’t be a problem if you just want your blog posts to be available without the blog bit.

The code you need would be basically like in the old Kirby 2 docs, adapted to Kirby 3:

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

Here is similar code for Kirby 3:

1 Like

That is totally working if I know the slug of the parent pages, but we want to have flat urls for every page.

By the way: For me it’s totally fine and logic how kirby works by default, and I think thats the way every website should, but those SEO People in my office think different :frowning:

Maybe something like:

$page = $kirby
  ->index()
  ->filterBy('uid', $slug)
  ->first() ?? site()->errorPage();

(Sorry, would have given a complete example, but writing code on a phone is really painful)

This obviously has to scan the whole website on every page load. Depending on the size of your website, this could be a problem for performance and, therefore, also for SEO :wink:

Note that in addition to the routes, it would also make sense to overwrite the URL method for every page to print the target URL.