Editable page redirects

###Why do I need editable page redirects?

  • You wat to create a menu item which redirects to its first child
  • You want to have an easy to remember url which links to an existing page
  • You want to have redirects which can be edited by editors within the panel

Creating editable page redirects with Kirby is really easy …

###Step 1: Blueprint
site/blueprints/redirect.php

title: Redirect
pages: true
files: false
fields:
  title:
    label: Title
    type:  text
  redirect:
    label: Redirect to page
    type:  page

Step 2: Controller

site/controllers/redirect.php
<?php

return function($site, $pages, $page) {

  if (!$page->redirect()->empty()) {
    // if 'redirect to page' is set, go for it
    $redirect = $page->redirect()->url();
  }
  elseif ($page->children()->visible()->count()) {
    // else if page has visible children, redirect to the first one
    $redirect = $page->children()->visible()->first()->url();
  }
  else {
    // fallback: redirect to homepage
    $redirect = $site->homePage()->url();
  }

  return array(
    'redirect' => $redirect,
  );
};

Step 3: Template

site/templates/redirect.php
<?php go($redirect) ?>
10 Likes

This is quite a bit more elegant than the solution I’ve been using. I’ll definitely give it a try. Thanks for sharing!

2 Likes

Now available as a plugin.

Kirby 2.3 is required.

Version 1.0

There’s a new version available on Github.

Special thanks to @benzin* for the suggestions and the massive help with thinking, coding and testing.


* I hope this is the right user account, because there’s no image or website in your profile.

2 Likes

You are most welcome! Glad to contribute, thanks for your work as well!

( You picked the right account, but I updated my details here anyways :wink: )

@flokosiol Once I have put the redirect folder in plugins, how to I enable it on specific pages?

Thanks

OK I see, I change the page template to Redirect. I have one problem though, I am redirecting my Articles page to the homepage. When I select a tag like: http://localhost/articles/tag:kirby that also gets redirected to the homepage. Is there a way to prevent that?

Thanks

Glad you got it working. The plugin doesn’t consider params at the moment and it probably won’t do it in the future. I think this is kind of an edge case and it’s almost impossible to find a general solution for this which works for all kind of params / use cases.

Instead I would use a custom template (maybe “articles”) for this. If params are set, display the stuff you want and if not redirect to the homepage.

You can find corresponding documentation here:
https://getkirby.com/docs/cheatsheet/helpers/params
https://getkirby.com/docs/cheatsheet/helpers/go

Try to figure it out and if you can’t find a working solution, let me know.

1 Like