Moving WordPress URL

8 Feb 2014

WordPress is one of the most used CMS, but there is some configuration data hardcoded in the database we should be aware of when we’re moving our site to another URL. Normally the main URL is coded on some configuration file like config.php, and every link is relative to that string, but this doesn’t happen on WordPress.

Assuming we are not moving our files nor database[1], but only changing our URL, we should start by defining the basics:

1
2
Old site: http://old_page.com
New site: http://new_page.net

First of all, edit your web server configuration

1
nano /etc/apache2/sites-available/example

and add a ServerAlias with the new website URL. Once the whole migration process is done and tested, we should consider removing the original ServerName in case the old link won’t be used anymore:

1
2
ServerName  old_page.com
ServerAlias new_page.net

Reload your web server:

1
/etc/init.d/apache2 reload

Your new URL must be working, but WordPress’s links will still point to the old site, which are hardcoded into the database. Now it’s time to switch to our database manager to execute these lines:

1
2
3
# Change MYWPDATABASE with your actual db name
UPDATE MYWPDATABASE.wp_options SET option_value = REPLACE(option_value,'old_page.com','new_page.net') WHERE option_id>0;
UPDATE MYWPDATABASE.wp_posts SET guid = REPLACE(guid,'old_page.com','new_page.net') WHERE ID>0;

Now every link has been changed with the desired URL, unless you hardcoded them in your posts or pages. Didn’t you? Maybe your client did 😉

1
UPDATE MYWPDATABASE.wp_posts SET post_content = REPLACE(post_content,'old_page.com','new_page.net') WHERE ID>0;

This would change every hardcoded link inside your posts’ content.

We should be done, but maybe there are some third-party plugins which also hardcode the URL in their settings. In this case, you should check for new tables created by these plugins in your database, and search for the old URL.

It’s also recommended to search for some configuration file containing the old URL inside your WordPress files:

1
grep -r "old_page.com" /var/www/example

[1] In case you are moving your WP files and/or database to another server, you should first dump your WordPress database, import it into your new server, then copy your WordPress folder, change the config.php file if you have changed any database configuration, and configure your web server. Also remember to check your permissions in case you didn’t do a secure copy with the scp command.