1

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

apaderno
  • 28,547
  • 16
  • 75
  • 90
homersheineken
  • 323
  • 6
  • 15

4 Answers4

1

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

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

danielson317
  • 3,121
  • 3
  • 28
  • 43
  • 1
    Right we have that installed (it already created url aliases), but how do you get it to regenerate aliases for just those certain nodes that need to have them changed? We have 15M nodes and I don't want to regenerate it for all of them. – homersheineken Aug 21 '12 at 14:21
  • If you have a list of those nodes you can dump them into a php array and write a quick script with them. Just call $node = node_load(nid); node_save($node); on each of them. – danielson317 Aug 21 '12 at 16:03
  • What about auto redirecting old alias to new? if old alias deleted search indexed links will lost their users, and 404 pages will come. – Rantiev Jul 25 '14 at 12:30
1

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

Vaibhav Jain
  • 354
  • 2
  • 8
  • Good feedback. I always forget to install the redirects because I always work on dev systems where they are not needed. – danielson317 Aug 21 '12 at 16:06
0

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);

}
Pez
  • 1,251
  • 16
  • 32
0
$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.

Jackson
  • 1,426
  • 3
  • 27
  • 60