How to get the full URL of a Drupal page?
In Drupal, you can use the url() function to get the full URL of a page.
In Drupal 7, you can use the url() function to get the full URL of a page. This code can be used in Drupal 7 modules, theme template files, or preprocess functions. For example, if you want to get the full URL of the current page, you can use the following code:
Example of getting the full URL of the current page in Drupal
<?php
$current_url = url('<current>', ['absolute' => true]);
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
You can also use the url() function to get the URL of a specific page by passing in the path of the page as the first argument. For example, if you want to get the URL of the "about" page, you can use the following code:
Example of using the url() function to get the URL of a specific page in Drupal
<?php
$about_url = url('about', ['absolute' => true]);The absolute option accepts a boolean value: true will return an absolute URL, while false will return a relative URL.
Note for Drupal 8/9/10: The url() function is deprecated and removed in modern Drupal versions. To get a full URL in Drupal 8+, use the url route generator service or Drupal\Core\Url::fromRoute(). For example:
<?php
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => 1], ['absolute' => TRUE])->toString();