Configure a variable domain name

By default, WordPress will store the website’s domain name in the database and it will use this value from the database to generate the URL’s when you use the site_url() function.

When you find yourself moving a WordPress website from one domain to another, or if you want to run your website locally to work on it, having this domain stored in the database can be cumbersome since the website will be pointing to the wrong domain name. Luckily we can override this setting by defining the WP_HOME and WP_SITEURL constants the wp-config.php.

Add the following to your wp-config.php directly after the top comment and before anything else:

$site_url = "http://".$_SERVER['HTTP_HOST'];

define('WP_HOME', $site_url);
define('WP_SITEURL', $site_url);

What this does is basically ask the server what the current host is (e.g.g mysite.com) and then override the value stored in the database by setting two WordPress constants, WP_HOME and WP_SITEURL.

By using this technique your website will be transferable over different domains.

Tagged with: