Drupal 6 Bulk Redirecting Urls

Trying to do bulk redirection through the path_redirect (D6) or redirect (D7) admin is... painful. You need to enter them one at a time. Staring down a list of a couple hundred can take a full day away from you. This handy function can be run through a drush script to redirect an array of paths anywhere you like in seconds.

Drush Script

Note that this is actually a drupal 6 tested function. It's not perfect, handling for things like query strings breaks down if you try run it multiple times. But for simple aliases, it can handle it all for you. Just give it an array of a source paths like 'my-title-here' and a destination like 'other/page', and watch the output. Note that it will tell you about failed redirects, so you will have to watch for them and fix those yourself.

This code is adapted from the path_redirect admin's form validate, so it should operate in a very similar way.

There's an options trigger as well that will allow you to retaining existing node aliases, or remove them if desired.

  1. function _bulk_redirect($redirects, $destination, $options){
  2.   $options += array(
  3.     'unset_node_aliases' => FALSE      
  4.   );
  5.   foreach($redirects as $source){
  6.     $redirect = array(
  7.         'source' => $source,
  8.         'language' => '',
  9.         'redirect' => $destination
  10.     );
  11.     $valid = TRUE;
  12.      
  13.     if( $path = drupal_get_normal_path($source) ){
  14.       $nid = str_replace('node/', '', $path);
  15.       if( is_numeric($nid) && $node = node_load($nid) ){
  16.         if( $options['unset_node_aliases'] ){
  17.           path_set_alias('node/' . $nid, NULL);
  18.         } else {
  19.           $valid = FALSE;
  20.           drupal_set_message('Can not redirect ' . $source . ' it is attached to an existing node:' . $node->title, 'error');
  21.         }
  22.       }
  23.     }
  24.      
  25.     if ($existing = path_redirect_load_by_source($redirect['source'], $redirect['language'])) {
  26.       if ($redirect['rid'] != $existing['rid'] && $redirect['language'] == $existing['language']) {
  27.         // The "from" path should not conflict with another redirect
  28.         $valid = FALSE;
  29.         drupal_set_message('Can not redirect ' . $source . ' it conflicts with an existing redirect', 'error');
  30.       }
  31.     }
  32.      
  33.     if (!valid_url($redirect['redirect']) && !valid_url($redirect['redirect'], TRUE) && $redirect['redirect'] != '<front>') {
  34.       //form_set_error('redirect', t('The redirect <strong>to</strong> path does not appear valid.'));
  35.     }
  36.      
  37.     // check that there there are no redirect loops
  38.     if (url(drupal_strtolower($redirect['source'])) == url(drupal_strtolower($redirect['redirect']))) {
  39.       drupal_set_message('Can not redirect ' . $source . ' it is an infinate loop.', 'error');
  40.       $valid = FALSE;
  41.     }
  42.      
  43.     if( $valid ){
  44.       drupal_set_message("Redirecting $source to $destination.");
  45.       path_redirect_save($redirect);
  46.     }
  47.      
  48.   }
  49. }
Tags: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.