Skip to content

SQL queries to change the domain name of a WordPress site

WordPress is a popular website builder. In the life cycle of a site, it is possible to change the domain name. Making such a change on a WordPress site requires changing the database information because the site URL is hard-written in multiple places in that database.

WordPress logo

Warning : these requests are effective for WordPress version 3.5, but they are not 100% guaranteed for future WordPress evolutions. You should make a backup of WordPress tables before using them blindly.

Practical information : by default the prefix for WordPress tables is “wp_” but this prefix can be modified during installation, in particular for security reasons. Therefore, it is necessary to adapt the following requests with the correct prefix.

Change the URL of the site (siteurl and home)

The URL of the site is recorded in the database, therefore this poses a problem when the site is migrated to another domain name. To modify the URL, you should use an SQL query which replaces the old URL with a new URL:

UPDATE wp_options
SET option_value = replace(option_value, 'http://www.ancien-site.com', 'http://www.nouveau-site.com')
WHERE option_name="home"
OR option_name="siteurl";

Change the URL of GUIDs (Global Unique Identifier)

The GUID is a unique identifier that serves as a referral URL for articles. It is used in particular by the readers of RSS feeds, in order to know if the URL has already been visited or not. If the URL of the site you have 2 possibilities:

  • Keep the same GUIDs and too bad if the URL no longer works
  • Change the URL of the GUIDs to the new site, but RSS readers will find new URLs and consider them new content. This can annoy your readers who will end up with many old articles considered to be new content.

Anyway, here is the procedure to change URLs for GUIDs:

UPDATE wp_posts
SET guid = REPLACE (guid, 'http://www.ancien-site.fr', 'http://www.nouveau-site.fr');

Change the URLs of the content of pages and articles (link, image, document, etc.)

The URLs contained in the pages and articles of a WordPress site are written in absolute and not in relative. To modify the URL, you must therefore use the following request:

UPDATE wp_posts
SET post_content = REPLACE (post_content, 'http://www.ancien-site.fr', 'http://www.nouveau-site.fr');

Change image URLs only

In the life of a site, there are several reasons for changing the URL of images:

  • Upload images to a specific CDN, especially to improve site performance
  • Host the images on cloud computing, such as the Amazon cloud

The query to change URLs of images in articles is as follows:

UPDATE wp_posts
SET post_content = REPLACE (post_content, 'src="https://www.ancien-site.fr', 'src="https://cdn.nouveau-site.fr');

And to modify the guid only for images, you must use this request:

UPDATE wp_posts
SET guid = REPLACE (guid, 'http://www.ancien-site.fr', 'http://cdn.nouveau-site.fr')
WHERE post_type="attachment";

Change the URLs of the meta data of posts

When a site changes URLs, it is useful to be able to update the URLs of the meta data with the following SQL query:

UPDATE wp_postmeta
SET meta_value = REPLACE (meta_value, 'http://www.ancien-site.com','http://www.nouveau-site.com');

Summary (all queries)

To summarize, here are all the SQL queries to perform to migrate a WordPress site from one domain to another.

# Changer l'URL du site
UPDATE wp_options
SET option_value = replace(option_value, 'http://www.ancien-site.com', 'http://www.nouveau-site.com')
WHERE option_name="home"
OR option_name="siteurl";

# Changer l'URL des GUID
UPDATE wp_posts
SET guid = REPLACE (guid, 'http://www.ancien-site.fr', 'http://www.nouveau-site.fr');

# Changer l'URL des médias dans les articles et pages
UPDATE wp_posts
SET post_content = REPLACE (post_content, 'http://www.ancien-site.fr', 'http://www.nouveau-site.fr');

# Changer l'URL des données meta
UPDATE wp_postmeta
SET meta_value = REPLACE (meta_value, 'http://www.ancien-site.com','http://www.nouveau-site.com');

Check that the old URL no longer exists

To ensure that the change of URL is successful, it is advisable to search for the old URL on all the tables in the database. If the old URL is still present, it suffices to manually modify the few recalcitrant data to finalize the change of URL.

Other requests

This list already contains several more or less essential requests to migrate a site to another domain name. If you know of others, don’t hesitate to share them in the comments of this article.