Redirect to Subpage

A client wants to keep an old url
angebot/anfordern
while the angebot is an empty page, what I want to redirect to its subpage angebot/anfordern.

To be able to use redirection multiple times, I wanted to create a blueprint redirect.yml and a template redirect.php

The blueprint:

fields:
  redirect:
    type: pages
    multiple: false
    required: true
    default: 'home'

the Template

$redirect = is_object($page->redirect()) && $page->redirect()->isNotEmpty() ? $page->redirect()->toPage()->url() : 'home';

go($redirect);

It works fine, if I do not use a subpage. But in that single case I want to use a subpage angebot/anfordern, which doesn’t work.
It also works if I test just a string in go-helper go('angebot/anfordern'). Anyone any ideas?

Random idea but try removing ->toPage()->url()

New code

$redirect = is_object($page->redirect()) && $page->redirect()->isNotEmpty() ? $page->redirect() : 'home';

Thanks for reply.
It just returns the UUID like
/Ml1Y9mst4Nb5KQ3Y
I tried several "random ideas " :sweat_smile:

So the angebot page uses this redirect tempalte?

Not the issue here, but your condition doesn’t make sense.

$page->redirect() returns a field object, not a page object
$page->redirect()->isNotEmpty() checking if the page is empty doesn’t make sure you have a page object

So better:

$redirect = ($re = $page->redirect()->toPage()) ?  $re->url() : 'home';

Thank you, but it still showes
“Error: Redirection error
An error occurred when connecting to myurl.de.
This problem can sometimes occur when cookies are disabled or refused.”

If you don’t execute the redirect, but

dump($redirect);

what do you get as output?

There was the problem. it was replying
https://myurl.de/angebot
anyhow, ther must have been the same UUID. I guess by manually creating pages by copy + paste
Thanks for your support!