Right now the url to my articles are like this:
mysite.com/home/article
While I want it to be displayed like this:
mysite.com/article
I understand that the reason for this is that the projects are stored under the “home” in the content folder. But I do not understand how I can solve this like the way I have described above.
texnixe
November 19, 2015, 1:48pm
2
You can use a router, check out the routing docs:
http://getkirby.com/docs/advanced/routing
Thanks, but I do not understand how they work. could you please give me an example?
texnixe
November 19, 2015, 1:52pm
4
You can just use the example in the docs, omitting the blog folder in urls: http://getkirby.com/docs/advanced/routing/#omitting-the-blog-folder-in-urls
Put the following code in your config.php and replace blog
with home
:
c::set('routes', array(
array(
'pattern' => '(:any)',
'action' => function($uid) {
$page = page($uid);
if(!$page) $page = page('blog/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
),
array(
'pattern' => 'blog/(:any)',
'action' => function($uid) {
go($uid);
}
)
));
Oh! Thanks! That work just like a dream!
Hey there, what if i want to hide a second or third part of an url?
Example:
mysite.com/artists/own-artists/name
to
mysite.com/artists/name
That works in the same way, you just have to add one level
<?php
c::set('routes', array(
array(
'pattern' => 'artists/(:any)',
'action' => function($uid) {
$page = page('artists/'. $uid);
if(!$page) $page = page('artists/own-artists' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
),
array(
'pattern' => 'artists/own-artists/(:any)',
'action' => function($uid) {
go('artists/' . $uid);
}
)
));
Perfect, thank you!! I didn’t get the full logic behind it, so i couldn’t figure it out by myself. Learned something new
There is one problem. The URL looks good now, but the wrong page gets returned. Instead of the artists page, the home page gets returned.
Well, i assume it doesn’t work because i have used the UID/URIs in the templates.
Example:
`<?php foreach(page('artists/own-artists')->children()->visible()->sortBy('surname', 'asc') as $artist): ?>
…
<?php endforeach ?>`
Maybe i better use the .htaccess to hide/redirect?
Make sure that all UID / URIs are correct, I corrected a typo above (missing s on artists)
I’ve tried the solution from the docs in order to omit the home folder in urls and could only make it work with an own expression for the dynamic placeholder.
c::set('routes', array(
array(
'pattern' => '([a-z0-9-]+)',
'action' => function($uid) {
$page = page($uid);
if(!$page) $page = page('home/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
),
array(
'pattern' => 'home/(:any)',
'action' => function($uid) {
go($uid);
}
)
));
After some basic testing, it seems that patterns with dynamic placeholders at URL root level did not work in my case. (Local MAMP Env.)
'pattern' => '(:any)'
Only with a previous parent folder, the pattern starts working:
'pattern' => 'parentfolder/(:any)'
Maybe this helps, if someone else has similar issues.