Manual:$wgServer

From MediaWiki.org
Jump to: navigation, search
General Settings: $wgServer
The base URL of the server.
Introduced in version: pre 1.1.0
Removed in version: still in use
Allowed values: URL prefix (protocol, host, optional port; no path portion)
Default value: (dynamically created)

Other settings: Alphabetical | By Function


The base URL of the server, including protocol and without the trailing slash (eg, http://www.mediawiki.org). Since 1.18 MediaWiki has also supported setting $wgServer to a protocol-relative url (eg, //www.mediawiki.org). This is used for supporting both HTTP and HTTPS with the same caches by using links that work under both protocols. When doing this $wgCanonicalServer can be used to set the full url including protocol that will be used in locations such as emails that don't support protocol relative urls.

This is used when producing fully-qualified URLs pointing to the wiki, for instance:

  • HTTP redirects on edit and to canonical URL spellings
  • print footer
  • links to articles from RSS/Atom feeds
  • and more!

Contents

Autodetection [edit]

When $wgServer is not set the default value is calculated automatically. Some web servers end up returning silly defaults or internal names which aren't what you want; for instance the ServerName directive in Apache's httpd.conf may not be set or detected properly by the system, leaving you with an unexpected http://localhost. It's always good to configure the web server properly. But you are also always strongly recommended to set $wgServer explicitly in your LocalSettings.php as there are other bugs that may arise from allowing it to be auto-detected, such as cache pollution.

History [edit]

Before 1.2.0 [edit]

Calculated by calling getenv( "SERVER_NAME" );.

1.2.0 [edit]

Switched to using PHP $_SERVER[] array instead of getenv(). Automatically work out port number.

$wgServer           = "http://" . $_SERVER["SERVER_NAME"];
if( $_SERVER["SERVER_PORT"] != 80 ) $wgServer .= ":" . $_SERVER["SERVER_PORT"];

1.3.0 [edit]

Added code to dynamically work out protocol. Used a sensible default when running from the command line.

# check if server use https: 
$wgProto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
 
if ( @$wgCommandLineMode ) {
        $wgServer = $wgProto.'://localhost';
} else {
        $wgServer           = $wgProto.'://' . $_SERVER['SERVER_NAME'];
        if( $_SERVER['SERVER_PORT'] != 80 ) $wgServer .= ":" . $_SERVER['SERVER_PORT'];
}
unset($wgProto);

1.3.8 [edit]

Fills in more details from the $_SERVER[] array.

if( isset( $_SERVER['SERVER_NAME'] ) ) {
        $wgServerName = $_SERVER['SERVER_NAME'];
} elseif( isset( $_SERVER['HOSTNAME'] ) ) {
        $wgServerName = $_SERVER['HOSTNAME'];
} else {
        # FIXME: Fall back on... something else?
        $wgServerName = 'localhost';
}
 
# check if server use https:
$wgProto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
 
$wgServer = $wgProto.'://' . $wgServerName;
if( isset( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] != 80 ) {
        $wgServer .= ":" . $_SERVER['SERVER_PORT'];
}
unset($wgProto);

1.5.0 [edit]

Adds some extra alternatives for $wgServerName for if the earlier tests fail, and checks whether the HTTPS protocol is on the default port before modifying it.

if( isset( $_SERVER['SERVER_NAME'] ) ) {
        $wgServerName = $_SERVER['SERVER_NAME'];
} elseif( isset( $_SERVER['HOSTNAME'] ) ) {
        $wgServerName = $_SERVER['HOSTNAME'];
} elseif( isset( $_SERVER['HTTP_HOST'] ) ) {
        $wgServerName = $_SERVER['HTTP_HOST'];
} elseif( isset( $_SERVER['SERVER_ADDR'] ) ) {
        $wgServerName = $_SERVER['SERVER_ADDR'];
} else {
        $wgServerName = 'localhost';
}
 
# check if server use https:
$wgProto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
 
$wgServer = $wgProto.'://' . $wgServerName;
# If the port is a non-standard one, add it to the URL
if(    isset( $_SERVER['SERVER_PORT'] )
    && (    ( $wgProto == 'http' && $_SERVER['SERVER_PORT'] != 80 )
         || ( $wgProto == 'https' && $_SERVER['SERVER_PORT'] != 443 ) ) ) {
 
        $wgServer .= ":" . $_SERVER['SERVER_PORT'];
}
unset($wgProto);

1.7.0 [edit]

The only change is that the port is not appended to the server name if it already contains a colon, or if the port is a standard one (80 or 443)

/** URL of the server. It will be automatically built including https mode */
$wgServer = '';
 
if( isset( $_SERVER['SERVER_NAME'] ) ) {
        $wgServerName = $_SERVER['SERVER_NAME'];
} elseif( isset( $_SERVER['HOSTNAME'] ) ) {
        $wgServerName = $_SERVER['HOSTNAME'];
} elseif( isset( $_SERVER['HTTP_HOST'] ) ) {
        $wgServerName = $_SERVER['HTTP_HOST'];
} elseif( isset( $_SERVER['SERVER_ADDR'] ) ) {
        $wgServerName = $_SERVER['SERVER_ADDR'];
} else {
        $wgServerName = 'localhost';
}
 
# check if server use https:
$wgProto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
 
$wgServer = $wgProto.'://' . $wgServerName;
# If the port is a non-standard one, add it to the URL
if(    isset( $_SERVER['SERVER_PORT'] )
    && !strpos( $wgServerName, ':' )
    && (    ( $wgProto == 'http' && $_SERVER['SERVER_PORT'] != 80 )
         || ( $wgProto == 'https' && $_SERVER['SERVER_PORT'] != 443 ) ) ) {
 
        $wgServer .= ":" . $_SERVER['SERVER_PORT'];
}

1.18 [edit]

The installer sets this variable in LocalSettings.php. However, if it is not set in LocalSettings.php, the server name will be detected at run time using the method WebRequest::detectServer().

See Also [edit]

Language: English  • français • 日本語 • русский