How can I change the URL to a subpage?


#1

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.


#2

You can use a router, check out the routing docs:

http://getkirby.com/docs/advanced/routing


#3

Thanks, but I do not understand how they work. could you please give me an example?


#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);
    }
  )
));

#5

Oh! Thanks! That work just like a dream! :blush:


#6

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


#7

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);
    }
  )
));

#8

Perfect, thank you!! I didn’t get the full logic behind it, so i couldn’t figure it out by myself. Learned something new :slight_smile:


#9

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?


#10

Make sure that all UID / URIs are correct, I corrected a typo above (missing s on artists)