2

Is it possible to take a field from my node and create a url alias from the content of that field?

Say I have a node type article. This gets its normal url via pathauto, but I also wish to be able to create a short url so I can go to example.com/LS and I will be redirected to the node.

How can this custom short url be created automatically?

migrated from stackoverflow.com Jul 18 '11 at 5:15

This question came from our site for professional and enthusiast programmers.

0

When you create a node they will always use the url node/[nid].

You can use the path auto module to create an url alias for the standard node. Path auto uses the token module to create the url alias for the nodes.

What you can do, is to create a token for the value of the field, and then setup path auto to use the contents of the field when creating the url alias.

Another thing you could do is to use the ShURLy module, to actual create short urls. You could make a custom module where you created or update the short url for the node when saved, and let ShURLy handle the redirects.

  • My node type has already been set up to use the title of the node as the alias like /articles/node-title-here and I'd like it to stay this way. But in addition to this it'd be cool if I could also have /[my_cck_field] to point to the article. Is this possible with the path auto module? – schwifty Jul 19 '11 at 20:40
  • Okay so I created a triggered rule that creates an additional alias based on a token when a node is created or updated. It works alright. It works alright except I'd like it to be case sensitive - and well...the alias doesn't redirect (of course) so maybe I should take a look at the ShURLy module. – schwifty Jul 19 '11 at 21:08
  • I decided it wasn't necessary to create the urls based on my cck field so the ShURLy module combined with Shorten URLs did the trick. – schwifty Jul 20 '11 at 14:31
  • @theory I am having the same issue. And I have installed these two module, and I wanted to auto generate short url. Can you suggest how can I do this? – Kalpesh Patel Feb 18 '13 at 5:25
0

You can use hook_node_insert and path_save like so:

function mymodule_node_insert {
  $nid = $node->nid;
  $path = array(
    'source' => 'node/' . $nid,
    'alias'  => 'my/other/path',
  );
  path_save($path);
}

This will add an additional alias after the node is created. This won't interrupt Pathauto, so you'll have both aliases active.

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.