I'm currently migrating a site to drupal. The old site has thousands of dynamic url which i can recognize via regex. those urls do exist in a slightly different form in the drupal page. unfortunately the url structure is too complicated for beeing processed direclty in an htaccess file.
Now I want all old urls to answer with a 301 respond redirecting to the new url.
Here is what I tried: 1st: implementing page_preprocess hook and sending an RedirectResponse to the new url if the incoming url matches my regex for the old urls.
2nd: implementing an event subscriber that does basically the same on the onRequest event.
In both cases I send the Redirect Respond like this:
$respond = new TrustedRedirectResponse($url,301); $respond->send(); die();
Both solutions do work if the user is logged in. In my local dev env, also if the user is not logged in. But on the live server it does not work when the user is not logged in.
Any ideas what I'm doing wrong? Or suggestions how to manage what I'm trying to do?