1

I have a site that regularly adds & removes event nodes based on a third party system. When these nodes are removed, any other articles that link to them get 404 pages. Is there a way I could intercept the 404 results for pages with a certain path and redirect them to the event landing page instead?

I have tried adding the following to template.php but I get a redirect loop

function MYTHEME_preprocess_page(&$vars)
{
    // Redirect expired events
    $status = drupal_get_http_header("status");
    if ($status == "404 Not Found" && preg_match('/\/events\//', request_uri())) {
        drupal_set_message('Sorry, this event is no longer available','error');
        drupal_goto('/events/');
    }
}
3

Use the Match Redirect module to redirect specific pages that matches urls.

Summary of drupal project page :

This module provides redirecting based on path patterns with wildcards. This functions much like how block page visibility works. You specify a pattern like "old-blog/*" and a target like "new-blog" and all pages under old-blog will be redirected to the page new-blog.

Features Pattern matching allowing wildcard redirects Redirect code choice (301, 302, etc) Filters out existing pages so they won't be redirected (unless overridden) Loop protection (no chaining redirects allowed)

  • Fantastic, that's exactly what I wanted. This will be very useful. – nexus_6 Feb 11 '15 at 12:18
1

You could just modify what you had implemented

function MYTHEME_preprocess_page(&$vars)
{
    // Redirect expired events
    $status = drupal_get_http_header("status");
    if ($status == "404 Not Found" && preg_match('/\/events\/.+/', request_uri())) {
        drupal_set_message('Sorry, this event is no longer available','error');
        drupal_goto('/events');
    }
}

This would redirect a path matching /events/ followed by at least one more character, which would prevent the redirect loop.

  • 1
    I would like to add I'd discourage the use of request_uri() since it will show the entire path from domain. I would probably use drupal_get_path_alias(). – Ambidex Oct 27 '16 at 12:52

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.