Search and Replace all URLs and links in WordPress, HTTP to HTTPS

Find and Replace all URLs or Text in a WordPress Database

Last updated on

Whether it’s a domain name change or a transition from HTTP to HTTPS, in this guide we will find and replace URLs (or any text) in your WordPress database using a few different methods.

SQL Query Examples

The best way to find and replace content in your WordPress database is by running SQL queries in either phpMyAdmin, shell command line or a PHP script. If you are not comfortable working directly with the database, we have included some plugins at the bottom of the page.

Always make sure to back up your WordPress database before running any queries or plugins.

Domain Change Example

We can use the MySQL replace function to replace any text in the WordPress database.

In the following queries, we will find and replace old_domain.com with new_domain.com, useful if you are migrating your WordPress site to a different domain.

There are a few different tables you should update if you are changing domain name:

wp_options – this is the WordPress configuration table, which contains your site URL in the home and siteurl fields.

UPDATE wp_options SET option_value = replace(option_value, 'old_domain.com', 'new_domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

wp_posts – this table contains all your post content in the post_content field. If you have any internal permalinks in your posts to other posts, you will need to replace the domain in all permalinks here. Also within this table is the guid field, containing the permalink of the post. Only change this field if you know what you are doing (see below).

UPDATE wp_posts SET post_content = replace(post_content, 'old_domain.com', 'new_domain.com');

wp_postmeta – this contains all your attributes and custom fields, including Advanced Custom Fields (ACF) in the meta_value field. If you have any permalinks in your custom fields, you will need to replace them here.

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'old_domain.com', 'new_domain.com');

wp_comments – contains all your post comments, which may contain permalinks to other posts on your site.

UPDATE wp_comments SET comment_content = replace(comment_content , 'old_domain.com', 'new_domain.com');

About the guid field – The Global Unique Identifier appears in this field in the form of a URL. You are not meant to change this field, even if your site URL changes. Think of the GUID as a unique hash. Altering it will mean that feedreaders will suddenly display all your content in the user’s reader again as new content. Only change this if you understand the possible repercussions. (read more)

UPDATE wp_posts SET guid= replace(guid, 'old_domain.com', 'new_domain.com');

HTTP to HTTPS Example

Another common WordPress URL change is when moving your site to SSL. The following queries will find and replace all instances of http://devanswers.co with https://devanswers.co.

There are a few different tables you should update if you are migrating to HTTPS:

wp_options – this is the WordPress configuration table, which contains your site URL in the home and siteurl fields.

UPDATE wp_options SET option_value = replace(option_value, 'http://devanswers.co', 'https://devanswers.co') WHERE option_name = 'home' OR option_name = 'siteurl';

wp_posts – this table contains all your post content in the post_content field. If you have any internal permalinks in your posts to other posts, you will need to replace the http:// part in all permalinks here. Also within this table is the guid field, containing the permalink of the post. Only change this field if you know what you are doing (see below)

UPDATE wp_posts SET post_content = replace(post_content, 'http://devanswers.co', 'https://devanswers.co');

wp_postmeta – this contains all your attributes and custom fields, including Advanced Custom Fields (ACF) in the meta_value field. If you have any permalinks in your custom fields, you will need to replace them here.

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://devanswers.co', 'https://devanswers.co');

wp_comments – contains all your post comments, which may contain permalinks to other posts on your site.

UPDATE wp_comments SET comment_content = replace(comment_content , 'http://devanswers.co', 'https://devanswers.co');

About the guid field – The Global Unique Identifier appears in this field in the form of a URL. You are not meant to change this field, even if your site URL changes. Think of the GUID as a unique hash. Altering it will mean that feedreaders will suddenly display all your content in the user’s reader again as new content. Only change this if you understand the possible repercussions. (read more)

UPDATE wp_posts SET guid= replace(guid, 'http://devanswers.co', 'https://devanswers.co');

Replace Text in Posts

Let’s say you are rebranding and changing your company name from “DevAnswers Co.” to DevAnswers Inc.”, you can use the SQL replace function exactly the same way.

There are a few different tables you should update if you want to replace all instances of text:

wp_options – this is the WordPress configuration table, which contains the name of your site in the blogname field.

UPDATE wp_options SET option_value = replace(option_value, 'DevAnswers Co.', 'DevAnswers Inc.') WHERE option_name = 'blogname';

wp_posts – this table contains all your post content in the post_content field.

UPDATE wp_posts SET post_content = replace(post_content, 'DevAnswers Co.', 'DevAnswers Inc.');

wp_postmeta – this contains all your attributes and custom fields, including Advanced Custom Fields (ACF) in the meta_value field. If you have any content in your custom fields, you will need to replace your text here also.

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'DevAnswers Co.', 'DevAnswers Inc.');

wp_comments – contains all your post comments, which may contain instances of the text you want to replace.

UPDATE wp_comments SET comment_content = replace(comment_content , 'DevAnswers Co.', 'DevAnswers Inc.');

What about the wp_links table?

I’ve read several other guides suggesting you alter the link_url field in the wp_links table. This now-deprecated table was used for the WordPress Links feature, discontinued in Dec 2012. You can probably ignore this field unless your WordPress install is really old.

Update the Database

Now that you understand which queries to run, we’ll go through a few different methods on how to run your queries.

Method 1 – phpMyAdmin

The easiest way to find and replace text in your WordPress database without having to install a plugin is through phpMyAdmin. Most shared hosts provide phpMyAdmin where you can administer your databases.

Log in to phpMyAdmin and click your database name in the left-hand pane.

phpMyAdmin database tree menu

Select the SQL tab and paste in your prepared queries.

Back up your database before running these queries because you cannot undo them!

Click Go and your query should run.

Method 2 – Command Line

If you have shell access to your web server, you can run your database queries directly in command line.

If using MySQL, log in using your WordPress database credentials.

mysql -u root -p database_name

Once you see the mysql> prompt, enter your SQL queries and press ENTER.

Back up your database before running these queries because you cannot undo them!

mysql> UPDATE wp_options SET option_value = replace(option_value, 'old_domain.com', 'new_domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0

mysql> UPDATE wp_posts SET post_content = replace(post_content, 'http://devanswers.co', 'https://devanswers.co');

Query OK, 0 rows affected (0.08 sec)
Rows matched: 354 Changed: 0 Warnings: 0

mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://devanswers.co', 'https://devanswers.co');

Query OK, 0 rows affected (0.01 sec)
Rows matched: 22 Changed: 0 Warnings: 0

mysql> UPDATE wp_comments SET comment_content = replace(comment_content , 'http://devanswers.co', 'https://devanswers.co');

Query OK, 0 rows affected (0.01 sec)
Rows matched: 10 Changed: 0 Warnings: 0

Once finished, exit MySQL.

mysql> exit

Method 3 – PHP Script

If you don’t have phpMyadmin or shell access, you can use a PHP script.

Simply enter your database credentials and place your query into the $sql variable.

Back up your database before running these queries because you cannot undo them!

<?php

$mysqli = db_connect('localhost','database_username','database_password','database_name'); 

$sql = "UPDATE wp_posts SET post_content = replace(post_content, 'http://devanswers.co', 'https://devanswers.co'"; 

function db_connect($host,$user,$pass,$db) {
 $mysqli = new mysqli($host, $user, $pass, $db);
 $mysqli->set_charset("utf8");
 if($mysqli->connect_error) 
 die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
 return $mysqli;
}

if ($mysqli->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error: " . $sql . "
" . $mysqli->error;
}

$mysqli->close();

?>

Method 4 – Use a Plugin

Our ethos is not to install plugins unless absolutely necessary! But if you are not comfortable working directly with the database, a plugin might be for you.

There are two plugins we’ve tested which are highly rated and reliable.

1. Search & Replace

This will search and replace URLs or any text. It also includes a backup facility.

Search & Replace

2. Better Search Replace

This performs the same task but has more features and a Pro upgrade option.

Better Search Replace

Let me know in the comments if this helped. Follow me on Twitter, Facebook and YouTube.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (2 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces