Easy way to update site URL when moving a WordPress blog to a new location

Let's say a WordPress blog previously accessible via http://old/ is now at http://new/. You've updated the Apache config, yet when accessing http://new/, you get redirected to http://old/, which no longer exists.

This is because WordPress still considers itself at the old URL. You could use the following SQL find/replace old URL instances with new, directly in the database:

1
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://old/', 'http://new/');

But wait, you're not done, there are more places to update..


There are probably references to the old URL elsewhere as well. The ones I found were in the post_content and guid columns of the wp_posts table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://old/', 'http://new/');
UPDATE wp_posts SET guid = REPLACE(guid, 'http://old/', 'http://new/');
 
SELECT COUNT(*) post_content FROM wp_posts WHERE post_content LIKE '%http://old%';
+--------------+
| post_content |
+--------------+
|            0 | 
+--------------+
 
SELECT COUNT(*) post_content FROM wp_posts WHERE post_content LIKE '%http://new%';
+--------------+
| post_content |
+--------------+
|           40 | 
+--------------+
 
SELECT COUNT(*) guid FROM wp_posts WHERE guid LIKE '%http://old%';
+------+
| guid |
+------+
|    0 | 
+------+
 
SELECT COUNT(*) guid FROM wp_posts WHERE guid LIKE '%http://new%';
+------+
| guid |
+------+
|   89 | 
+------+

If you're running WordPress MU, the process is the same, just need to specify the right table as each WPMU blog has its own. See this post for how to enumerate WPMU blog numbers and URLs.

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).