ozke
ozke

Reputation: 1620

Drupal. Get path alias from current url without the installation's folder arguments

I'd like to retrieve the current page path alias without the installation's folder arguments. I'm using:

drupal_get_path_alias(request_uri())

But this returns installation/whatever/actual/path and I want to retrieve the actual/path only no matter what installation/whatever is.

Thanks in advance :)

Upvotes: 4

Views: 25289

Answers (7)

Timofey Drozhzhin
Timofey Drozhzhin

Reputation: 4714

Fastest solution.

substr(drupal_get_path_alias(request_uri(), 1), strlen(base_path()));

To pass it into an argument

$arg = explode('/', substr(drupal_get_path_alias(request_uri(), 1), strlen(base_path())));

Upvotes: 0

Al.
Al.

Reputation: 301

I tend to plump for the full url instead to avoid any problems that arise when base_path() isn't just '/'.

$url = (($_SERVER['HTTPS'])?"https://":"http://")  .  $_SERVER['HTTP_HOST'] . url($_GET['q']);

or even more simply:

$url = url($_GET['q'], array('absolute'=>true));

Upvotes: 0

chim
chim

Reputation: 8573

$current_page_url = drupal_get_path_alias( implode( '/', arg() ) );

also works

Upvotes: 1

ozke
ozke

Reputation: 1620

Found it. It was actually a mix of both suggestions:

$current_path = drupal_get_path_alias($_GET["q"]);

Thanks though.


Update: the previous solution doesn't always work

Someone suggested using an alternative method:

str_replace(base_path(), '', drupal_get_path_alias(request_uri(), 1));

But, is there any way of doing the same without using the slightly expensive str_replace?

Thanks

Upvotes: 8

Brice Favre
Brice Favre

Reputation: 1527

Maybe you can use base_path() and str_replace like this :

str_replace (base_path(), '', drupal_get_path_alias(request_uri()), 1);

The base_path is saved in the database.

Upvotes: 1

ceejayoz
ceejayoz

Reputation: 180014

Have you tried $_GET['q']?

Upvotes: 0

Kevin
Kevin

Reputation: 13226

The path of the node you are on?

http://api.drupal.org/api/function/drupal_get_path_alias/6

if ($node || (arg(0) == 'node' && arg(2) != 'edit')) {
   $system_path = 'node/'.arg(1);
   $current_path = drupal_get_path_alias($system_path);
}

That code will fire on node pages and tell you the page alias.

For more information, you can dump out $_GET and look at the 'q' query string value.

Upvotes: 2

Related Questions