How to get the path of a node from the node ID in Drupal
By David Pratt / Tags: drupal, seo / 9 Comments / Published: 03-12-09
I’ve just stumbled across a very useful internal function in Drupal that I thought I’d share here.
If you have a node that you know the node ID for, but you don’t know what the full clean url is (or how to generate it), then there is a handy function you should know about called drupal_lookup_path() that will get the full clean (pathauto) path for you. For example, a typical Drupal relative path such as:
/node/9999
Is easy to generate if you know the node ID, but if you pass it through the drupal_lookup_path function like so:
drupal_lookup_path('alias',"node/".$node->nid)
Then you will get the full clean path of the node based on the rules set out in the pathauto module. For example:
/band/future-of-the-left
This is very important for SEO. If the exact same content/page can exist on two different urls on your site, then you will run into problems with duplicate content and will have to start using things like the canonical link element to fix a problem that shouldn’t have been created in the first place.
Subscribe via RSS
Twitter
David Pratt December 9th, 2009 at 11:46 pm
Ah sneaky. I’ll look into that.