Move WordPress CMS from one URL / Domain to another with SQL-Query

0
46

If you are changing the URL / Domain of your websites or if your want to duplicate one of your websites, you have to refactor the Database of WordPress to work with that new Domain. You can do this with a few simple Querys.

For those of you who just want to copy and paste: Take the following queries and replace http://www.oldurl and http://www.newurl with your old and new Url.

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

 

Details about WordPress moving querys

For this of you who want to know what these Querys / understand what has to be done to move a WordPress-system I explain each query.

1.) Tell WordPress what his base-url is

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

The first query changes two options in the General-Tab of the WordPress-Settings.

Options you have to change to move wordpress to new domain
These options are changed by the query above.

The first one is the base-address of the WordPress-System. The second is the Url of the Homepage.

2.) Refactor all WordPress GUIDs by query

UPDATE wp_posts SET guid = replace(guid, ‘http://www.oldurl’,’http://www.newurl’);

The GUID is the address of a single post or page (or other posttype). You can see it in the backend directly under the titel of the post / page.

Changing the GUID of a wordpress post by query
Guid of a wordpress post has to be changed to match the new URL

As they contain the old URL / Domain, they have to be refactored. We replace the old Domain with the new one.

3.) Refactoring post-content after domain change

Our old domain can be plastered all over our website-content. Whenever we link a picture or another website from content there is a text-link which has to be replaced. This is done by the third query:

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

4.) Replacing occourances in Meta-Values

Meta-Values are used for all fields and values which are not part of the standart-wordpress post. For example if your theme add additional options to a post these options are usually stored in meta-values. Of course these meta-values can also contain links. So we check for our old-domain and replace with the new one:

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

Is there anything else to do when moving wordpress?

These are the most usually places which have to be adressed. Depending on your website there might be some other tables you have to refactor.

For example if you allow your users to comment on your posts they might link to another page. In that case you have to check comment-content for occourances of your old domain.

Let me know if you have trouble with moving your website in the comments.

LEAVE A REPLY

Please enter your comment!
Please enter your name here