Beware this content is over 5 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.

If you’ve ever had to move a WordPress website from one folder to another or one server to another or from a development environment to a production/live environment - whichever scenario you’ve probably had the trouble of ensuring the URLs survive the transition.

Normally, you can handle this in one of two ways. The first is do a fresh install of WordPress at the new location, exporting your old data from the previous site and then re-importing into the fresh install. This carries with it it’s own set of issues but is a viable option. The main problem though is the concurrency of the two databases when it comes time for fixing or maintenance.

The second is you can copy the whole site and transplant it, database, files and all. The problem with this approach is unless your previous environment was identical to the location you were moving it to, your links would now be broken.

The easiest way to fix this (I’m found so far) is to execute a few SQL statements on the database in question. It’s effectively a find and replace. The main tables to take note of (as of WordPress 3.3.x) is wp_posts and wp_options.

Note: I’m assuming you’ve backup anyway before running these statements on your database.

1
2
3
4
UPDATE wp_posts SET guid = REPLACE(guid, 'http://localhost/', 'http://www.example.org/');
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://localhost/', 'http://www.example.org/');
UPDATE wp_posts SET post_excerpt = REPLACE(post_excerpt, 'http://localhost/', 'http://www.example.org/');
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://localhost/', 'http://www.example.org/');

Any questions? Drop a message below.