Wordpress

Moving a WordPress Blog to a New URL

March 25, 2009   ·   By   ·   5 Comments   ·   Posted in Wordpress

This probably isnt something that comes up too often for the average user – your URL is your identity – so switching them isnt a great choice.  In client work, however, this comes up all the time – development work takes place locally on your computer WAMP/MAMP, or on your server, where the client can keep an eye on things.

Once its ready, however, you need to move the site to their live server, and you need it to work right, and in a hurry.  Oftentimes the client site is already live, and they’re just pushing a redesign out – you need things to not go wrong at this stage.  Even as a personal user, there are a number of things that can go wrong while moving a site to a new URL, and nothing is less satisfying than figuring out why your site wont work after the move.

The Basics:

First, you need to understand how wordpress works – this might seem silly, but I’ve had a number of clients who can’t really conceptualize it – simply because they’ve never really worked with web applications before.  WordPress obviously runs on a database – mysql.  While it would be nice to just be able to move files over to the new server, the database must come along too.  The database stores all of your posts, pages, and comments, as well as any settings that you’ve made during the setup process.  It also stores some important options – most importantly, your blogs URL.   That’s not really within the scope of this post, so the quick version is this – using your database access tool (I like Sequel Pro when I can get external database access, though nearly all hosting setups come with PHPMyAdmin) you need to dump all the wordpress database tables, and import them into the new host’s database.  After that is done, you can put your files (all the wordpress core files, along with your theme, of course) on the new server.

What needs to be updated?

The one file you’ll need to make sure to update is wp-config.php.  You need to make sure that the host has been changed to match your new site’s database settings, and you need to make sure that your database username, password, and database name are all properly updated.

At this point, your blog should be working – but theres a problem – when you load it up, you’re immediately redirected to the old blog url.

picture-11To get around that, you’ll have to get into the database.  WordPress blogs hold nearly all of their settings in a table called options (note – the default name for this table will actually be wp_options, so you’ll have to tack on your database prefix, which is found in wp-config.php, to the front of options to find the right table).  There are 2 main options that need to be changed – the first has an option_name of ‘siteurl’, and the second is ‘home’.  Once you’ve changed these 2 values, you should be able to go to your site at the new url, and login as you normally would.

Other Potential Problems

There are still some problems that you can run into after this point.  The first, and most common relates to your upload directory.  Often, after moving to a new url (or moving to a new host), you’ll start getting a permissions error when you try to use the image uploader.  This happens because of a field in the options table called “upload_path”.  The upload path specifies where to store your uploads (obviously), but when wordpress is installed, this is automatically generated  relative to your filesystem (not web) root.  The easiest way to fix it is to go to the Miscellaneous settings page (Under the settings menu on your dashboard), and change the value there to ‘wp-content/uploads’.

Updating Your Posts

This last piece is a little tricky, and can be dangerous – so please – BACK UP YOUR DATABASE before you try this.  It’s not very hard to mess this up, and its very hard to fix (unless of course, you’ve got a backup lying around).  If you’re moving a blog to a new url, but the old URL had a significant amount of post content on it, you want to make sure that all images now properly link to the new server.  WordPress uses full image paths to link to images inside of posts by default, so if your old url (or the files on it) dissappear, you’ll end up with lots of broken images on the new one.  Also, you want to make sure that any internal linking you did inside of posts is maintained.

The Magic of MySQL find/replace

To get all our old image and link urls properly updated, we’re going to use a mysql find and replace function.  Heres how it looks:

update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

Lots of thanks to this page for teaching me this in the first place.  It has saved me hours.

Here’s how I’d use this mysql snipped to change all the urls in my post content on this blog, if I were moving it to a blog found at http://www.someblog.com/petersnewblog.

update wp_posts set post_content = replace(post_content,'http://yourcodegarage.com/blog/','http://www.someblog.com/petersnewblog/');

A quick rundown of what we did there – The table we want to work with is wp_posts, and the field is post_content.  So we’re taking every instance of

http://yourcodegarage.com/blog/

that is found in the post_content field of the wp_posts table, and replacing it with

http://www.someblog.com/petersnewblog/.

This replaces partial strings, so every time an image used to link to

http://yourcodegarage.com/blog/wp-content/uploads/2009/03/someimage.jpg,

it now links to

http://www.someblog.com/petersnewblog/http://www.someblog.com/petersnewblog/wp-content/uploads/2009/03/someimage.jpg.

The same goes for links.

Thats it – good luck!

5 Comments
  1. Thanks for the MySQL replace syntax. That will come in handy. Another way to change the post links is to edit the ‘my_db_backup_file.sql’ prior to the restore process. Back up the .sql file before editing. I used TextPad (any robust text editor will do) and replaced the old_url with the new_url within the sql file. Used MySQL Administrator (PHPMyAdmin should work as well) to restore the backup sql and voila! everything is in place with the new_url intact. As stated above this can be dangerous. BACKUP, BACKUP, BACKUP. CHECK YOUR WORK, CHECK YOUR WORK, CHECK YOUR WORK.

  2. Thank you so much… my WP upload paths got screwed and your post helped a lot… The Magic of MySQL find/replace was the KEY!

  3. Also, make sure you take Kerry’s advice and BACKUP your DB before running find and replace…

  4. Just needed to update image URLs, worked first time, really good tip. Thanks

  5. Just run this script instead: http://interconnectit.com/124/search-and-replace-for-wordpress-databases/

    I’ve used it successfully.

    It’s linked from the bottom of http://codex.wordpress.org/Moving_WordPress

    Hope this helps.

Submit a Comment