0

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 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 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?

  • I am using Drupal 8.2.6 and I stumble upon same issue, in which '404 error' was getting logged even though my custom eventSubscriber was being called. Referring to the comments given in redirect module, I set a number higher than 32 and it worked as expected!!! public static function getSubscribedEvents() { // This needs to run before RouterListener::onKernelRequest(), which has // a priority of 32. Otherwise, that aborts the request if no matching // route is found. $events[KernelEvents::REQUEST][] = array('yourCustomFunction', 34); return $events; } – Prajeesh Dec 29 '17 at 17:37
0

Never use die() or send responses like that yourself. The event you have has a method to set a response object use that, see how the redirect.module does it for example

  • I tried that too. Didn't change a thing. – Leonhard Brunner Aug 26 '17 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.