Change URL in WordPress database when migrating

Run these queries after migrating to make image paths point to new server.

2015.02.23 update added the find and replace technique and an external tool.

Few different methods to replace URLs in WordPress database. Handy when deploying.

Method 1: search and replace in a text editor

Just export the file, open in your favorite text editor and do a find and replace. This is not very ‘scientific’ and you need to do the importing and exporting process.

Method 2: with SQL queries

Here’s few SQL queries to replace strings in WordPress database. I’ve always ran these in phpMyAdmin, choose the DB and then the SQL tab, paste these in one by one:

UPDATE wp_posts SET guid = replace(guid, '','');
UPDATE wp_posts SET post_content = replace(post_content, '', '');
UPDATE wp_postmeta SET meta_value = replace(meta_value, '', '');

External tools

Dave in the comments links to this cool looking little tool: Search-Replace-DB.


  • davemac says:

    One thing to be aware of is that URLs need to be changed in serialized arrays. This tool does that:

  • Hiljá says:

    Thanks Dave for the tip, I edited the article a bit.

  • richard blezer says:

    I’m still baffled by the fact that WP uses full url paths in the database. Why?
    Why are media files hardcoded to a site url? Every other CMS I use, is using relative paths /images/uploads/file.jpg
    when moving a WP site form say to some 300 db entries needs to changed. Really stupid.

Club-Mate, the beverage →