There are a few reasons why WordPress does not use $_SERVER['SERVER_NAME']
as the default value for WP_SITEURL
and WP_HOME
.
Firstly, $_SERVER['SERVER_NAME']
only returns the hostname of the server, not the full URL of the WordPress site. For example, if your WordPress site is installed in a subdirectory (e.g. http://example.com/blog
), $_SERVER['SERVER_NAME']
will only return example.com
, not the full URL of the site. In this case, using $_SERVER['SERVER_NAME']
as the default value for WP_SITEURL
and WP_HOME
would cause the URLs of your site to be incorrect.
Secondly, using $_SERVER['SERVER_NAME']
as the default value for WP_SITEURL
and WP_HOME
can cause problems if your site is accessed through multiple hostnames or aliases (e.g. if you have multiple domains or subdomains pointing to the same WordPress installation). In this case, using $_SERVER['SERVER_NAME']
as the default value would cause WordPress to use only one of the hostnames or aliases, which may not be the one you want to use as the primary URL for your site.
For these reasons, WordPress does not use $_SERVER['SERVER_NAME']
as the default value for WP_SITEURL
and WP_HOME
. Instead, it uses the default values of http://localhost
for WP_SITEURL
and http://localhost/wp
for WP_HOME
, which can be changed by setting the values of these constants in the wp-config.php
file. This allows you to specify the correct URLs for your site, regardless of whether it is installed in a subdirectory, accessed through multiple hostnames or aliases, or hosted on a server with a different hostname.