Drupal Bulk Update Url Alias

6.8k views Asked by At

I have a set of nodes (about 200) that need to have their url alias updated since we changed the setup; from "site.com/things-to-do/title" to "site.com/guides/title"

I tried using VBO, but when I select the nodes, click Update Url Alias, and then execute, nothing happens.

Although I'd rather not do a straight DB update, I also tried: "UPDATE url_alias set dst = replace(dst, 'things-to-do', 'guides') WHERE url_alias LIKE 'things-to-do/%';

Thanks

4

There are 4 answers

3
danielson317 On BEST ANSWER

Path auto should fix your problem: http://drupal.org/project/pathauto

It will give you the option to delete existing aliases and regenerate them.

0
Pez On

Here's a quick script I wrote that solves the problem. It does a simple find and replace on the alias and then creates a redirect.

You need the url_alias and path_redirect modules installed, this is for Drupal 6. This isn't the cleanest syntax (it was written quickly) but it works.

$string_to_replace = "foo";
$replacement_string = "bar";

//Get an array containing all aliases that need to be updated
$query = "SELECT * from url_alias where dst like '%$string_to_replace%'";
$result = db_query($query);
$paths = array();
while ($row = db_fetch_array($result)){
  $paths[] = $row;
}

foreach($paths as $path){

  //Determine the new path
  $path['new_dst'] = str_replace($string_to_replace,$replacement_string,$path['dst']);

  //Update the existing url_alias
  $query = "UPDATE url_alias SET dst = '".$path['new_dst']."' WHERE pid = " . $path['pid'];
  $result = db_query($query);

  //Create and save a redirect
  $redirect = array(
    'source' => $path['dst'],
    'redirect' => $path['src'],
  );
  path_redirect_save($redirect);

}
1
Vaibhav Jain On

Apart from using the pathauto module stated above, you should also use path redirect module - http://drupal.org/project/path_redirect - so that you can redirect your old links to the newly created ones, else your old links will die out. You can also use Global redirect - http://drupal.org/project/globalredirect

0
Jackson On
$nids = Drupal::entityQuery('node')
    ->condition('type', 'CONTENT_TYPE')
    ->execute();
$nodes = Node::loadMultiple($nids);

foreach($nodes as $node) {
    $node->path->pathauto = 1;
    $node->save();
}

I hope above code will solve the issue.