title: Drupal: How do I programatically create a URL alias for a node that already has an alias on node save?
body: <p>I have a custom module that implements hook nodeapi to execute some code when the node is created or updated.</p>
  
  <p>Basically I want to create an alias based off of the automatically generated alias on node save or update.</p>
  
  <p>Right now I'm using a call to path_set_alias and I only want to do this with a specific type of content, "product".</p>
  
  <p>Here is my nodeapi call to get me started</p>
  
  <pre><code>function product_url_helper_nodeapi(&amp;$node, $op, $a3 = NULL, $a4 = NULL) {
  
   if($node-&gt;type == 'product'){
    switch($op){
  
       case 'insert':
        _create_alternate_url($node);
  
       break;
  
       case 'update':
        _create_alternate_url($node);
       break;
  
       case 'view':
         //do nothing
       break;
  
    default:
    break;
  
    }
   }
  
   return;
  }
  </code></pre>
  
  <p>Then I have this function, the one I'm trying to get to save my second URL alias for me.</p>
  
  <pre><code>function _create_alternate_url($node){
  $aliasExists = db_fetch_object(db_query("SELECT count(dst) as total FROM {url_alias} WHERE dst = 'alternate/".$node-&gt;path."'"));
  if($aliasExists-&gt;total == 0){
  
      $product_url = $node-&gt;path;
   $alternate_url = "alt/" . $node-&gt;path;
   $default_node_path = "node/" . $node-&gt;nid;
  
      path_set_alias($default_node_path, $alternate_url, 0, '');
  
    drupal_set_message("Created Alternate path for Product: " . $node-&gt;title . " &lt;br/&gt; Path &lt;a href='/" . $default_node_path ."'&gt;" . $default_node_path . "&lt;/a&gt; is now aliased by &lt;a href='/" . $alternate_url . "'&gt;". $alternate_url ."&lt;/a&gt;");
   }
  </code></pre>
  
  <p>This doesn't set the alias though, it just creates a duplicate of the product's original alias.  So If i started off with my product being "Green Fern".  I would save it, and it would use pathauto to generate products/green-fern then after call my module code and make an alias "alt/products/green-fern" and still make it point back to the "node/nid" path.</p>
  
  <p>However, when I run this code a duplicate in the database is created.  So I save Green Fern one time and all of a sudden I see two duplicate records at the end of the url_alias in the database. "products/green-fern" and "products/green-fern"</p>
  
  <p>I feel like I'm thinking about this in a much too comlpex way.  My client is aware of the SEO hit they get when making more than one alias point to the same node, they just want it to do this.  Halp!</p>