Hi,
I am trying to create a route to remove /blog from the url of blog articles. I have managed to do it successfully for category and tag pages, but I am struggling with the article urls.
So far I have this:
'routes' => [
[
'pattern' => 'blog/(:any)',
'action' => function ($uid) {
$page = page('blog')->children()->find($uid);
if ( $page ) {
return page('/') . $page->slug();
}
}
],
This doesn’t redirect the page, but gives me a blank screen with only the page slug at the top.
Any ideas?
Thank you.
Joke
This pattern listens to the standard URL, from this one you don’t want to return the page, but redirect to the URL without blog
[
'pattern' => 'blog/(:any)',
'action' => function($uid) {
go($uid);
}
]
Then you need a second route that listens to the pattern (:any)
.
Like here: Routing | Kirby
But translated to Kirby 3
Hi,
Thanks for your reply, but I am still mystified. The url changes, but I am getting the error page. If I add a pattern for (:any) it all breaks on /blog.
[
'pattern' => 'blog/(:any)',
'action' => function ($uid) {
go($uid);
}
],
[
'pattern' => '/(:any)',
'action' => function ($uid) {
go($uid);
}
],
It’s also breaking something in my sitemap router. Not sure if that is related.
Joke
The second route may not start with a slash in the pattern and it has to return a page, not also redirect.
Complete example here:
As I said, if you use the new syntax in your config.php, it will/should work:
<?php
return [
'debug' => false,
'routes' => [
[
'pattern' => '(:any)',
'action' => function($uid) {
$page = page($uid);
if(!$page) $page = page('project/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
],
[
'pattern' => 'project/(:any)',
…
Only replace project
with blog
Wonderful. Now it’s working.
Thank you very much.