4

Is it possible to rewrite the url in a 404 redirect according to the page which is actually being displayed ?

For example, if the non-existent url "/fzenk" is requested, and you have configured the default 404 error page to be the front-page, the url in the address bar after the redirect should be front-page's.

I tried Global Redirect module but it didn't work. Is it possible to do the 404 redirect = URL-rewrite with another module or should we request a new feature ?


Update:

While it is possible to do it, it seems to be a bad idea to rewrite 404 pages url.

5

Thanks to Krister Andersson answer, I seem to have got it right.

function THEME_preprocess_page(&$variables, $hook) {
   $status = drupal_get_http_header("status");  
   if($status == "404 Not Found") {
      // get the configured 404 error page url :
      $not_found_url = variable_get('site_404');
      unset($_GET['destination']);
      drupal_goto($not_found_url);
   }
}

It will rewrite the url according to the configured default 404 error page, it works with multilingual variable too.

4

If you would like to redirect all your 404 requests to the frontpage I think you could use the template_preprocess_page() hook and drupal_get_http_status() to check for a 404:

function THEME_preprocess_page(&$variables, $hook) { 
   $status = drupal_get_http_header("status");  
   if ($status == "404 Not Found") {      
      header("Location: http://exampel.com/");
      drupal_exit();
   }
}  
  • The solution you provide is working, but it doesn't handles i18n redirection to the current language frontpage. I think it could be fixed by adding conditions with the language global variable. However, I would like a more generic way allowing to rewrite the URL according to the path configured in "Site information" error pages fields. – B2F Jun 30 '12 at 12:17
  • 1
    Plus the header() drupal way is drupal_goto – B2F Jun 30 '12 at 12:29
  • I was just pointing you a direction that could solve your problem, the code wasn't tested I just assumed it would work and yes, drupal_goto() should be used over a header() directive=) – Cyclonecode Jun 30 '12 at 14:06
1

I couldn't seem to get the 403 redirect working with the methods suggested in this thread, so I used a custom menu entry instead, added the path to the native drupal error redirect page, and Bob's your uncle. =o)

/**
 * Implements hook_menu().
 * 
 */
function MYMODULE_menu() {

  $items = array();

  // On the admin/config/system/site-information form, set the 404 and 403 
  // redirects to MYMODULE-custom-error/40x
  $items['MYMODULE-custom-error'] = array(
    'page callback' => 'MYMODULE_custom_error',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
  );

  return $items;
}

/**
 * Utility function to redirect users to the home page on 404 and 403 errors; 
 * note the settings on admin/config/system/site-information
 */
function MYMODULE_custom_error($error) {
  unset($_GET['destination']);
  drupal_goto();
}
0

For Drupal 6, here is a slightly modified code:

function THEME_preprocess_page(&$variables) { 
  $status = drupal_get_headers();
  if(preg_match('/404 Not Found/', $status)) {      
    unset($_GET['destination']);
header("Location: /pagenotfound");
    exit;
  }
}

Here we couldn't use drupal_goto() for redirection. Because this results in infinite loop.

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.