Simple redirects with a structure field

I have a small question, I would like to set up redirects for search engines. There are in principle 2 possibilities:

  1. the retour plugin, but here I have had numerous problems, occasionally the database deletes itself etc. The problems are known

  2. the cookbook recipe of @texnixe, here I am a bit bothered by the fact that I have to create the pages really what is sometimes associated with a lot of effort.

Therefore, I was wondering if it is not simply about a structure field. But I have some problems here. Especially if the website is on localhost in a subfolder. My demo instance is on localhost:8888/pb6, so my code doesn’t work here. I’m generally wondering if I have the right approach here, or what I’ve written together is total nonsense. Is there a “Kirby-Like” solution for this?

$current_path = $_SERVER['REQUEST_URI'];
foreach ( $site->redirects()->toStructure() as $redirect ) {
  if ( $current_path == $redirect->from()->value() || $current_path == $redirect->from()->value() . '/' ) {
    go($redirect->to()->value());
  }
}
redirects:
  type: structure
  label: Redirects
  fields:
    from:
      type: text
      label: Von
    to:
      type: text
      label: Zu

What are users supposed to add in the from and to fields?

I would probalby use routes or where are you putting your code snippet?

To my knowledge, I’ve never written a recipe about redirects :thinking:? What are you referring to?

Ohh, sorry, that was from @distantnative, I’m almost used to it since most cookbook recipes come from you :see_no_evil:

I imagine it so that in the from field, for example, /oldpage is entered and in the to field /newpage. Just quite simple redirects for search engines like Google, if for example a page was newly created and the URLs have changed.

The snippet is directly in header.php

Reference: Editable redirects | Kirby CMS

I’d create an array from the from values in a route pattern, then redirect to the equivalent to value.

I.e. pluck all from values from the structure field, create a pattern from that array and you are good to go.

Old KIrby 2 example, but logic is the same: Dynamic Routes Question - #4 by texnixe

Then inside the route, you fetch the corresponding to value from the structure field.

I would suggest a third possibility and from my point of view the favorite one (if you use Apache, but Nginx might have it also): RewriteMap.

It basically let you do this in your server configuration or .htaccess:

RewriteMap mymap "txt:/path/to/file/map.txt"
RewriteRule "^/(.*)" "${mymap:$1}"

And the file map.txt looks like:

Von1 Zu1
Von2 Zu2
Von3 Zu3

Sounds as if that needs a hardcode path which won’t work in different environments. While server side config is always better, it’s just not as flexible.

Plus you would have to generate this file in a hook whenever the structure field changes. Not a big deal, but still.

That’s how I see it too unfortunately, of course I could have the file generated via a hook, but the .htaccess variant doesn’t seem the best to me. Effectively I’m looking for something between the fixed templates and the powerful Retour plugin.

In principle, my code works, but I think it could be much better. The example of K2 I understand unfortunately not quite.