How to change the domain/URL for your WordPress site

It is possible to programatically change the URL for your WordPress site. This comes in handy if you need to migrate your WordPress site to a different location, or if you want to work on your WordPress site on a local environment.

To do so, just paste the following two lines into your wp-config.php file:

define('WP_HOME', 'http://www.mydomain.com');
define('WP_SITEURL','http://www.mydomain.com');

You can copy these two lines anywhere in the file above the comment which reads:

/* That's all, stop editing! Happy blogging. */

The WP_HOME option defines the URL where your WordPress core files reside, the WP_SITEURL option defines the URL typed into the browser to reach your WordPress site.

Developing your WordPress site on a local environment

An example of when these two lines of code could come in pretty handy is if you want to work on your WordPress site on a local environment, which is very much recommended. I develop my sites locally using MAMP Pro, which of course means the URL needs to be different to that of the live version of the site. Let’s look at an example of this:

My live URL is www.mydomain.com, so I set-up my site on MAMP Pro using the URL dev.mydomain.com. I use the prefix dev. as a way of identifying my local environment. Using a bit of PHP, I can identify whether the URL contains dev. and then set the URL accordingly. So here it goes:

if(strstr($_SERVER['HTTP_HOST'], "dev.") != false) {
    define('WP_HOME', 'http://dev.mydomain.com');
    define('WP_SITEURL','http://dev.mydomain.com');
}

All done. You can now develop the site on your local environment and WordPress will use your local URL’s specified above. Accessing the site from any other URL without dev. will use the original URL’s, set under the settings > General page in WordPress.

← Back to Blog