I'm currently migrating a site to Drupal
Drupal. The old site has thousands of dynamic urlsURLs for which i can recognize via regexI cannot use a regular expression to handle them. Those urls doesURLs exist in a slightly different form in the Drupal
pageDrupal site.
Unfortunately the urlURL structure is too complicated for beeingbeing processed direcltydirectly in ana .htaccess
.htaccess file.
Now I want all the old urlsURLs to answer withreturn a 301 respond redirectingredirect to the new urlURL.
HereThis 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.
- I implemented
hook_page_preprocess()
and sent aRedirectResponse
to the new URL when the incoming URL matches the regular expression for the old URLs - I implemented an event subscriber that does basically the same on the onRequest event
In both the cases I send, the Redirect Respond like this:redirect response is sent with the following code.
$respond = new TrustedRedirectResponse($url,301);
$respond->send();
die();
$respond = new TrustedRedirectResponse($url,301);
$respond->send();
die();
Both solutions do work ifwhen the user is logged in-in. In my local dev envdeveloping site, it works also ifwhen the user is not logged in-in. But onOn the live server, it does notdoesn't work when the user is not logged infor anonymous users.
FunnyThe funny thing is that, even ifwhen the redirectredirection works (logged in user)for logged-in users, the Drupal
log showshows a page not foundPage not found entry for that urlURL. So it seems that even with the eventsubscriberevent subscriber solution, Drupal
handelsDrupal handles the 404 part first, and then executes my code.
Any ideas what I'mWhat am I doing wrong? Or suggestions how to manage what I'm trying to do
Should I implement this differently?