0

I came across this site and I was wondering why WordPress isn't using this as default:

define('WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME']);
define('WP_HOME',    'http://' . $_SERVER['SERVER_NAME']);

So nobody has to change this values when changing the domain. Is there any reason or disadvantages of using this?

2 Answers 2

1

With these constants defined, your WP site will work both with and without the preceding www. (as well as on any other URLs that correctly resolve to the site, like hosting-provided staging URLs, etc), which search engines will penalize as duplicate content.

5
  • well, but how to prevent this? I cant see the difference to define('WP_SITEURL', 'http://www.example.com'); (except the dynamic url)
    – maysi
    Jan 24, 2015 at 17:08
  • 1
    you can prevent this by not using these constants, instead defining it using the fields under Settings > General as intended. Jan 24, 2015 at 17:11
  • but when I use Settings → Preferred domain in the Google Webmaster tools I won't have these problems. Am I right?
    – maysi
    Jan 24, 2015 at 17:15
  • based on my admittedly limited understanding of it, yes - I believe that is correct. Jan 24, 2015 at 17:20
  • ok. thanks :) 1 last question: When I use $_SERVER['SERVER_NAME'] but remove the possible www. with str_replace() it would work without penalization?
    – maysi
    Jan 24, 2015 at 17:24
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.