1. 8.2.x core/globals.api.php base_url
  2. 8.0.x core/globals.api.php base_url
  3. 8.1.x core/globals.api.php base_url
  4. 8.3.x core/globals.api.php base_url
  5. 6.x developer/globals.php base_url
  6. 7.x developer/globals.php base_url

The base URL of the Drupal installation.

See also

drupal_settings_initialize()

File

developer/globals.php, line 22
These are the global variables that Drupal uses.

Code

global $base_url;

Comments

geerlingguy’s picture

[From the D6 API comments:]

Base URL will begin with https:// if the current page is being accessed through the HTTPS protocol. This depends on the availability of $_SERVER['HTTPS'] which may not be available if a reverse proxy or load balancer is in use.

Also, the $base_url does not include the trailing slash, like so:

http://www.example.com

If you're using the $base_url to construct a link, make sure you add the trailing slash, like so:

  $link = $base_url . '/node/add';
D34dMan’s picture

Instead of hardcoding the forward slash ('/'), use global variable $base_path instead.

<?php
global $base_url;   // Will point to http://www.example.com
global $base_path;  // Will point to at least "/" or the subdirectory where the drupal in installed.
$link = $base_url . $base_path . 'node/add';

?>
But in case Drupal is running behind proxy then this will fail and you will have to probably use the string instead (like 'http://www.example.com/').
corbacho’s picture

$base_url already contains $base_path, as you can see in the 8.x doc (same for 7.x) https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drup...

alanom’s picture

If Drupal is in a subdirectory, it includes the path to the subdirectory when follows

global $base_url;
AlexBorsody’s picture

$link = $GLOBALS['base_url'] . '/node/' . $node->nid;

as with any super global variable you can access through $GLOBALS array.
here is a link to a node id.

mwallenberg’s picture

As a side note, creating a link to a node is better done with l(). This example will not work on a site with clean URLs turned off.

yookoala’s picture

Since both url() and l() are affected by i18n site url prefix settings, you cannot get correct path to a file with url('filepath'). You should use $base_path instead:


// an example to retrieve the url to a png file in module
echo $base_url . '/' . drupal_get_path('module', 'custom') . '/images/foobar.png';

Anybody’s picture

// an example to retrieve the url to a png file in module
echo $base_url . base_path() . drupal_get_path('module', 'custom') . '/images/foobar.png';
rjacobs’s picture

In regards to the example above, I believe this is incorrect. Concatenating $base_url and $base_path will most likely create redundant path structures in cases where drupal is in a sub directory (as $base_url will already contain the $base_path minus the "/").

Anyway, I think file_create_url() may actually be the preferred method when building links to "shipped files", as it will take care of all the "base" parts universally:

// an example to retrieve the url to a png file in module
echo file_create_url(drupal_get_path('module', 'custom') . '/images/foobar.png');
fngatia’s picture

Thanks this worked for me:

file_create_url(drupal_get_path('module', 'custom') . '/images/foobar.png');
Jaypan’s picture

Since both url() and l() are affected by i18n site url prefix settings, you cannot get correct path to a file with url('filepath'). You should use $base_path instead:

Actually you should use file_create_url().

wirka’s picture

What about if I would like to get base path including domain name? For example: I would like to get http://domain.com/drupal where drupal is the Drupal installation directory.

jackenmail’s picture

Nikdhil Mdohfan’s picture

<php
//$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $_SERVER['HTTP_HOST'].'/node/<nid>';
?>
pasi.jarnstedt’s picture

Using $_SERVER is wrong, it does not work if your server is behind forward Proxy or something similar.

This code would result requests to be made to http://server.domain.local/ or something similar.

$GLOBALS['base_url'] will give you more predictable results.