I'm currently migrating a site to
Drupal. The old site has thousands of dynamic urls which i can recognize via regex. Those urls does exist in a slightly different form in the
Unfortunately the url structure is too complicated for beeing processed direclty in an
Now I want all old urls to answer with a 301 respond redirecting to the new url.
Here is what I tried: 1st: Implementing hook_page_preprocess() 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.
Funny thing is, even if the redirect works (logged in user), the
Drupal log show a page not found entry for that url. So it seems that even with the eventsubscriber solution,
Drupal handels the 404 part first, and then executes my code.
Any ideas what I'm doing wrong? Or suggestions how to manage what I'm trying to do?