Migrate Wordpress to a new Server or Directory

If you decide to change the URL or link location of your WordPress Blog due to a new domain or moving to a sub-directory, here are some simple steps that should be done to ensure proper migration without breaking links.

To update WordPress options with the new blog location, use the following SQL command:

UPDATE wp_options SET option_value = replace(option_value, 'http://www.olddomain.com', 'http://www.newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.olddomain.com', 'http://www.newdomain.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.olddomain.com', 'http://www.newdomain.com');

9 comments for Migrate Wordpress to a new Server or Directory

Joe Brunno's picture

This worked perfectly! Thank...

This worked perfectly! Thank you so much!

Lydia's picture

Really great ! These three...

Really great !
These three command lines is all you need to have your database running again.

In all the WP migration advices I found, no ones talks about the fact that, in posts everywhere URLs have to be changed ! Plus don't try to change it by hand, it will take forever.
I also tried to dump the tables, open the scripts and find and replace "old url" by "new url", but somehow there was always something messing up.

So thanks for these simple 3 sql commands that go a long way...

Lydia

Richard's picture

Lydia, Your welcome! I’m...

Lydia, Your welcome! I’m happy it helped you!

Andy's picture

sorry, but where do i have to...

sorry, but where do i have to put these commands?

thanks for your help

Richard's picture

Andy, in your favorite...

Andy, in your favorite Database Administration tool such as phpMyAdmin.

Trevor's picture

I tried migrating my blog to...

I tried migrating my blog to a new server by copying all directories and the database over. I got the blog running but when I click on an article it give a 404 error. The domain is exactly the same. I just flipped DNS. Are there any database queries that need to be run to make something happen that I'm unaware of?

Richard's picture

Hey Trevor, Nope this works...

Hey Trevor,

Nope this works for me.

Make sure that any other plugins you have installed don’t have a reference to the old URL. Search the whole database.
Clear your Cache

Onno marsman's picture

If you are interested in this...

If you are interested in this post, you might also want to take a look at the ezMigrate plugin for Wordpress:
http://techblog.triptic.nl/ezmigrate-plugin-for-wordpress/

Travis Watson's picture

Just wanted to say that I use...

Just wanted to say that I use this code so much and I just keep coming back. THANK YOU so much!

Any chance you could update the post and put all lines in the same code block to make copying a bit easier?

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.