36

I need to get the full URL of the currently opened page. I use the following code, but it returns http://localhost/mysite/page_name.

$path = isset($_GET['q']) ? $_GET['q'] : '<front>';
$link = url($path, array('absolute' => TRUE));

It's not enough for my code to complete; I want to get the values that are passed with the URL as http://localhost/mysite/page_name/value1=value&value2=value.

How can I do that?

0

6 Answers 6

34

Have you tried using request_uri()?

$_SERVER['HTTP_HOST'] . request_uri();

It should do the trick. I haven't tried it myself, and I may be off a bit, but I suggest you give a try. From JavaScript, you could use the window.location.href variable.

5
  • great trick i used $_SERVER['REQUEST_URI'] to get the url but the when i print the url: sitename/pagename?field_empf_salaryfrom_tid=All&field_emp_date_value[value][year]=&field_emp_date_value[value][month]= How can i remove sitename/pagename Apr 9, 2012 at 12:44
  • 2
    @abd You're probably looking for $_SERVER['QUERY_STRING']
    – Clive
    Apr 9, 2012 at 12:48
  • Can i Put every value from the url in a variable? Like the value of (field_empf_salaryfrom_tid=All) in $s Apr 9, 2012 at 15:19
  • I suggest adding protocol to the answer with isset($_SERVER["HTTPS"]) ? 'https' : 'http';
    – Augusto
    Sep 11, 2018 at 11:02
  • request_uri() even works on 404 pages, that's a really good thing.
    – leymannx
    Jun 19, 2019 at 8:33
32

Here's how to get the absolute URL for the current page, using the Drupal API:

$current_url = url(current_path(), array('absolute' => TRUE));

If you need the extra (non-q) querystring parameters as well, you can use:

$current_url = url(current_path(), array(
  'absolute' => TRUE, 
  'query' => drupal_get_query_parameters(),
));
7

You can use the following code.

global $base_root;
$current_url = $base_root . request_uri();
echo $current_url;
3

Try $base_url variable, provides Drupal installation URL without "/" and current_path() it returns path of current page being viewed.

global $base_url;
$current_path = $base_url . '/' . current_path();
1

I think the best way to do that is using url() API like this

url('your/drupal/path', array('absolute' => TRUE));
2
  • The question is asking for a generic page, not one for which the path is known.
    – apaderno
    Dec 11, 2017 at 11:45
  • Ok, you can replace your/drupal/path¡with current_path(), as @david-thomas wrote before. Dec 13, 2017 at 13:52
1

The question is pretty old but chiming in for people that might get there for a more recent version of Drupal.

You can get the current full-path for a page with:

$path = \Drupal::service('path.current')->getPath();
$queryParams = \Drupal::request()->query->all();

$path . "?" . http_build_query($queryParams);

Really handy for dynamically changing the value of certain query parameters like a page for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.