Change the basedomian of your blog

Sometimes you have to change the base domains of your blog. Therefore there are the following possibilities (ATTENTION: These snippets are thought to be used with a single site installation):

Backend settings

If you have access to the Backend of your WordPress installation, you can change the basedomain of your blog under:

Settings -> General -> WordPress Address (URL)

and

Settings -> General -> Site Address (URL)

This option is useful if you want to move your site to another domain. The other side of the coin is, that you have to change the domains before moving the site and that your old site won’t be available from this moment on until the new site is ready.

wp-config entries

You can add the following lines to your wp-config.php to set the siteurl and home-URL. But that’s not a good idea because in this case you can’t use the Backend anymore to change these values.

define('WP_HOME','http://local.ask-sheldon.com');
define('WP_SITEURL','http://local.ask-sheldon.com');

Nevertheless it’s a good way to correct the domains temporarily to get into the settings if you accidentally changed the settings wrong. After correction you can remove the constants.

Database changes

But sometimes it is necessary to change these values direct in the database (f.e.: moving installation, buildup a development environment …). In these cases you can use a phpAdmin installation or you can directly write queries against the database. The relevant table is wp_options. The following queries can be used to look for and change the necessary table fields:

Show values for siteurl (WordPress Address) and home-URL (Site Address):

mysql> SELECT * FROM wp_options WHERE option_name LIKE 'siteurl' OR  option_name LIKE 'home';

This should result in a view like this:

+-----------+-------------+------------------------------+----------+
| option_id | option_name | option_value                 | autoload |
+-----------+-------------+------------------------------+----------+
| 2         | home        | http://www.ask-sheldon.com   | yes      |
| 1         | siteurl     | http://www.ask-sheldon.com   | yes      |
+-----------+-------------+------------------------------+----------+
2 rows in set (0.00 sec)

Now you can change the values with this query:

mysql> UPDATE wp_options SET option_value = 'http://local.ask-sheldon.dev' WHERE option_name = 'siteurl' OR option_name = 'home';

After this the table entries from above should look like this:

+-----------+-------------+------------------------------+----------+
| option_id | option_name | option_value                 | autoload |
+-----------+-------------+------------------------------+----------+
|         2 | home        | http://local.ask-sheldon.dev | yes      |
|         1 | siteurl     | http://local.ask-sheldon.dev | yes      |
+-----------+-------------+------------------------------+----------+

Now you need to check out if there are any direct connections to the old domain inside your blog posts:

mysql> SELECT * FROM  wp_posts WHERE post_content LIKE '%ask-sheldon.com%';

If there are any findings, you can search and replace them via:

mysql>  UPDATE wp_posts SET post_content = REPLACE(post_content,'www.ask-sheldon.com','local.ask-sheldon.dev');

WordPress Commandline Tool

Another option is the use of the WP-CLI tool (http://wp-cli.org/). This tool has a wide range of commands to deal with your WordPress installation (http://wp-cli.org/commands/).

The one you need to replace all occurrences of your old domain in the database is:

$> wp search-replace 'http://www.ask-sheldon.com' 'http://local.ask-sheldon.dev'

Or if you want to change the domains only:

$> wp option update siteurl 'http://local.ask-sheldon.dev'
$> wp option update home 'http://local.ask-sheldon.dev'

A description of the installation of wp-cli can be found here.

Further information

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.