steffcarrington
steffcarrington

Reputation: 404

How to get current page URL in Drupal?

I'm trying to output the current page URL on a Drupal site (Drupal 7), I've tried the following...

<?php print current_path(); ?>

Which outputs the page name but not the URL, any ideas on how to output the full URL too?

Upvotes: 2

Views: 8798

Answers (5)

Matoeil
Matoeil

Reputation: 7289

in drupal9

$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

Upvotes: 1

Davide Casiraghi
Davide Casiraghi

Reputation: 18087

On D7 if you want to get the current path you can use:

$path = drupal_get_path_alias();

So if your current URL is:

www.test.com/hello/world

You will get:

'hello/world'

Upvotes: 0

Sanjeev Chauhan
Sanjeev Chauhan

Reputation: 21

You can use

<?php 
    global $base_url;
    print $base_url . '/' . current_path(); 
?>

Upvotes: 2

Tim
Tim

Reputation: 485

If you want to keep things done "the drupal way", you can use current_path() or request_path().

https://api.drupal.org/api/drupal/includes!path.inc/function/current_path/7 https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7

You can also use $base_url instead of relying on $_SERVER.

The solution durbpoisn gave will give you the URL in the browser. If that is an alias, you can use drupal_get_normal_path() to get the internal path

https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7

Upvotes: 1

durbnpoisn
durbnpoisn

Reputation: 4669

In straight PHP you can use this:

$curPathName = $_SERVER["DOCUMENT_ROOT"];
$currentURL = $_SERVER["HTTP_HOST"];

I don't see why that wouldn't work. That will give you the path and the URL.

Upvotes: 2

Related Questions