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.

Prerequisites

Ideally you should have access to your WordPress database either via phpMyAdmin or shell command line. If don’t have access to either of these, you can try running a custom PHP script or plugin.

Warning: Back up your database before running any queries or scripts!

Which Tables to Update?

It’s useful to know which tables and fields you are updating and why.

  • wp_options (table)
    • home (field) and siteurl (field) – the URL for your site. Update this field if you are changing the site URL such as moving domains or changing to HTTPS. If you installed WordPress into your document root, these fields should be the same.
  • wp_posts (table)
    • post_content (field) – This is where all post and page content is stored. Any links in your posts and pages will be in this field and should be replaced if you are changing your site URL.
    • 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)
  • wp_postmeta (table)
    • meta_value – Where posts and pages store their fields and attributes, for example, any custom fields you have added, including any fields set by the plugin Advanced Custom Fields (ACF).
  • wp_comments (table)
    • comment_content (field) – Contains all post comments from your users and your replies, which may contain links to other posts on your site.
  • wp_links (deprecated table)
    • link_url (deprecated field) I’ve seen other guides showing this field. It was used for the WordPress Links feature, discontinued in Dec 2012. You can probably ignore this unless your WordPress install is really old.

SQL Query Examples

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

Domain Change Example

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

UPDATE wp_options SET option_value = replace(option_value, 'old_domain.com', 'new_domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, 'old_domain.com', 'new_domain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'old_domain.com', 'new_domain.com');
UPDATE wp_comments SET comment_content = replace(comment_content , 'old_domain.com', 'new_domain.com');

HTTP to HTTPS Example

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

UPDATE wp_options SET option_value = replace(option_value, 'http://devanswers.co', 'https://devanswers.co') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, 'http://devanswers.co', 'https://devanswers.co');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://devanswers.co', 'https://devanswers.co');
UPDATE wp_comments SET comment_content = replace(comment_content , '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 replace function exactly the same way.

UPDATE wp_posts SET post_content = replace(post_content, 'DevAnswers Co.', 'DevAnswers Inc.');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'DevAnswers Co.', 'DevAnswers Inc.');
UPDATE wp_comments SET comment_content = replace(comment_content , 'DevAnswers Co.', 'DevAnswers Inc.');

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 with 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.

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 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.

$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 (1 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