Getting the URL of a node in Drupal 7

59,123

Solution 1

You can use the url() function:

$options = array('absolute' => TRUE);
$nid = 1; // Node ID
$url = url('node/' . $nid, $options);

That will give you the absolute path (i.e. with http://example.com/ in front of it), with the URL aliased path to the node page.

Solution 2

You can also try drupal_lookup_path('alias',"node/".$node->nid).

Solution 3

Also you can get it by

$path=drupal_get_path_alias('node/'.$nid);

absolute path for nid

url('node/' . $node->id(), ["absolute" => TRUE]);

Solution 4

You can also use the l() function.

  l(t('Link text'), 'node/123', array('options' => array('absolute' => TRUE)));
Share:
59,123
Vishal Khialani
Author by

Vishal Khialani

I am a web developer and I work on drupal most of the time. I have travelled the world and worked in china, Taiwan and Indonesia for 7 years. Please feel free to connect with me at my BLOG

Updated on August 11, 2021

Comments

  • Vishal Khialani
    Vishal Khialani over 2 years

    Goal: To send an email with a list of URLs generated from nodes.

    In my custom module I have managed to get the node id which the user wants and I now want to get the URL of each node to put into my email.

    I searched the db and used google but I can't seem to find the right solution.

    It seems we need to construct the URL something like this:

    <?php
    global $base_url;
    $link=$base_url."// few more parameters 
    
  • robomc
    robomc over 11 years
    Is there some problem with this method? This seem more useful, and less obvious, than the approved answer.
  • Akarsh Satija
    Akarsh Satija over 10 years
    You are not getting the question! please read the question carefully before answering
  • cambraca
    cambraca over 9 years
    What if the node has no alias? also, this will return a relative path.
  • cambraca
    cambraca over 9 years
    What if the node has no alias? also, this will return a relative path.
  • Yuseferi
    Yuseferi almost 9 years
    @cambraca yes it is return raw path something like node/3
  • scotself
    scotself almost 9 years
    Use in conjunction with $base_url global variable.
  • William Turrell
    William Turrell over 4 years
    drupal_lookup_path() isn't always directly available, use url() instead.