Drupal 7 – Several ways to get current URL

There are a few ways to get the current URL in Drupal 7. First, we can get the $_GET[‘q’] by the current_path() function. But it will ignore the language prefix if you are working on a multilingual site.

Examples of current_path():

http://example.com/node/306
returns “node/306”
http://example.com/drupalfolder/node/306
returns “node/306” while base_path() returns “/drupalfolder/”
http://example.com/path/alias
returns internal path such as “node/306” as opposed to the path alias
http://example.com/en/path/alias
returns internal path such as “node/306” as opposed to the path alias and language prefix is ignored

 

There is also another function called request_path() which may also help you to get the current path with language prefix.

Examples of request_path():

http://example.com/node/306
returns “node/306”
http://example.com/drupalfolder/node/306
returns “node/306” while base_path() returns “/drupalfolder/”
http://example.com/path/alias
returns “path/alias” as opposed to the internal path
http://example.com/en/path/alias
returns en/path/alias
http://example.com/index.php
returns an empty string (meaning: front page)
http://example.com/index.php?page=1
returns an empty string

Another function to get the current URL is request_uri() which returns the $_SERVER[‘REQUEST_URI’] value.

Done =)

Reference:

Advertisements

5 thoughts on “Drupal 7 – Several ways to get current URL”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s