47

I need to be grabbing the URL of the current page in a Drupal site. It doesn't matter what content type it is - can be any type of node.

I am NOT looking for the path to theme, or the base url, or Drupal's get_destination. I'm looking for a function or variable that will give me the following in full:

http://example.com/node/number

Either with or without (more likely) the http://.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • drupal_get_destination is the solution since you know the domain name and if you are coding for the same domain, you can use it! – Bhavin Joshi Apr 16 '13 at 04:43
  • Related: [How do I get the full URL of the current page?](http://drupal.stackexchange.com/q/27880/1908) at Drupal SE – kenorb Mar 03 '15 at 23:01

9 Answers9

53

drupal_get_destination() has some internal code that points at the correct place to getthe current internal path. To translate that path into an absolute URL, the url() function should do the trick. If the 'absolute' option is passed in it will generate the full URL, not just the internal path. It will also swap in any path aliases for the current path as well.

$path = isset($_GET['q']) ? $_GET['q'] : '<front>';
$link = url($path, array('absolute' => TRUE));
Eaton
  • 7,405
  • 2
  • 26
  • 24
  • 1
    according to your link, drupal_get_destination() returns "destination=/node/number" or, for /node/number?destination=bla, "destination=bla". passing this to url() doesn't seem to work to me. – ax. Apr 03 '09 at 13:23
  • 1
    Doh. You're correct. I've updated the snippet with the correct code. Thanks! – Eaton Apr 03 '09 at 20:31
  • Cool, but I had to add: `$echo link;` for a facebook app to work! Thanks! –  Feb 06 '11 at 18:24
37

This is what I found to be useful

global $base_root;
$base_root . request_uri();

Returns query strings and it's what's used in core: page_set_cache()

mikeytown2
  • 1,744
  • 24
  • 37
  • 1
    Note that this method won't work if your site is not at the domain root. For example, if your base URL is http://example.com/folder, this method will result in http://example.com/folder/folder/page. – Brandon Gano Sep 12 '12 at 00:22
  • If that is the case then there is a bug in core. see that the code comments in http://api.drupal.org/api/drupal/includes!bootstrap.inc/function/conf_init/6 "Build $base_root (everything until first slash after "scheme://")." – mikeytown2 Sep 12 '12 at 00:36
  • Another thing that could be causing issues that you're seeing if if you're web server is passing the wrong values in for $_SERVER see http://api.drupal.org/api/drupal/includes!bootstrap.inc/function/request_uri/6. – mikeytown2 Sep 12 '12 at 00:42
19

You can also do it this way:

$current_url = 'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

It's a bit faster.

kenorb
  • 155,785
  • 88
  • 678
  • 743
ThomasR
  • 191
  • 1
  • 2
14

Try the following:

url($_GET['q'], array('absolute' => true));
kenorb
  • 155,785
  • 88
  • 678
  • 743
ax.
  • 58,560
  • 8
  • 81
  • 72
12

This method all is old method, in drupal 7 we can get it very simple

    current_path()

and another function with tiny difference

request_path()
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
  • The question is about getting the *full* URL, i. e. something like `http://example.com/node/number` . – ax. Sep 24 '12 at 11:16
4

I find using tokens pretty clean. It is integrated into core in Drupal 7.

<?php print token_replace('[current-page:url]'); ?>
Yo-L
  • 487
  • 1
  • 4
  • 12
3

The following is more Drupal-ish:

url(current_path(), array('absolute' => true)); 
kenorb
  • 155,785
  • 88
  • 678
  • 743
0

For Drupal 8 you can do this :

$url = 'YOUR_URL';
$url = \Drupal\Core\Url::fromUserInput('/' . $url,  array('absolute' => 'true'))->toString();
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
-1

Maybe what you want is just plain old predefined variables.

Consider trying

$_SERVER['REQUEST_URI'']

Or read more here.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108