How to get the full URL of a Drupal page?
When building a custom block or hook in Drupal, you often need to reference the current page's absolute URL. The distinction here is critical: you aren't looking for the relative path like /node/123, nor are you interested in the base URL (http://example.com). You specifically need the full string http://example.com/node/123.
Drupal provides a dedicated global variable to solve this immediately. The variable $base_url contains the complete URL of the current page, including the protocol (http or https) and the specific path segment. It is the most direct way to retrieve the full address within a theme template or a render array callback.
// Inside a theme template (e.g., tpl.html.twig) or PHP hook
$full_url = $base_url;
// Example output: http://example.com/node/42
Using $base_url is generally preferred over concatenating the base domain with a path because it handles subdirectories and custom base URLs automatically. If your site is installed at http://example.com/subdir, $base_url correctly reflects http://example.com/subdir/current-page.
If you are working within a render array, specifically inside a hook_views_pre_render() or similar context where the view has not yet fully resolved its URL, you might encounter empty values. However, in standard theme layers like preprocess_page, $base_url is reliably populated by Drupal's router.
There are scenarios where you might need to construct the URL manually using PHP functions rather than relying on a global variable. This approach is useful when you need to manipulate specific components of the URL, such as appending query parameters or stripping fragments before displaying them. In these cases, url() can generate an absolute URL based on a relative path.
// Generate full URL from a relative path
$path = 'node/' . $nid;
$full_url = url($path, ['absolute' => TRUE]);
// Result: http://example.com/node/42
The url() function is powerful because it respects the current site's configuration. It automatically detects whether your site is configured for HTTP or HTTPS and adjusts the scheme accordingly. This prevents issues where a hardcoded http:// prefix clashes with a server running on SSL.
One common pitfall developers face is confusing $base_url with the output of drupal_get_base_path(). The latter function returns only the path portion (e.g., /node/123) without the domain or protocol. If your code assumes it has the full URL but receives just a path, link generation will break, and redirects may fail. Always verify whether you need the scheme and host included in your string.
Another consideration involves security and sanitization. While $base_url comes from Drupal's internal router and is generally safe for display within templates, constructing URLs manually requires ensuring that any variables inserted into the path are properly escaped to prevent XSS vulnerabilities. When using url(), pass sensitive data only as query parameters if necessary, and never inject raw user input directly into the path segment without validation.
If you find yourself migrating from older Drupal versions or dealing with a multi-site configuration where base paths vary wildly, relying on $base_url remains the most robust strategy. It abstracts away the complexity of domain switching and subdirectory structures, ensuring that your custom blocks or theming logic works consistently regardless of how the site is deployed.
For developers transitioning between ecosystems, it is worth noting that while Drupal handles this via global variables and specialized helper functions, Laravel offers a similar utility through url()->full(). If you are ever evaluating frameworks for a new project, understanding how each handles absolute URL generation can inform your architectural decisions. For instance, if you need to integrate Drupal logic into a Laravel application or vice versa, knowing the standard patterns in both environments helps bridge the gap. You might explore resources like https://laravelcompany.com to see how modern PHP frameworks approach URL routing and generation, though the implementation details differ significantly between Drupal's global scope and Laravel's service container.
Ultimately, sticking with $base_url for most use cases keeps your code clean and resilient. Only reach for manual construction when you need specific control over the URL components or are operating in a non-standard rendering context where global variables might not be populated as expected.