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?


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.

| improve this answer | |
  • 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 – abd Apr 9 '12 at 12:44
  • 2
    @abd You're probably looking for $_SERVER['QUERY_STRING'] – Clive Apr 9 '12 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 – abd Apr 9 '12 at 15:19
  • I suggest adding protocol to the answer with isset($_SERVER["HTTPS"]) ? 'https' : 'http'; – augusto Sep 11 '18 at 11:02
  • request_uri() even works on 404 pages, that's a really good thing. – leymannx Jun 19 '19 at 8:33

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()));
| improve this answer | |

You can use the following code.

global $base_root;
$current_url = $base_root . request_uri();
echo $current_url;
| improve this answer | |

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();
| improve this answer | |

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

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

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.