WordPress 更换域名后如何替换数据库中旧的 URL

在 WordPress 网站更换域名后,需要将 MySQL 数据库中所有旧的 URL 替换为新的 URL。接下来,我们来看一下要怎么操作。

通过 PHPMyAdmin 执行 SQL 语句进行替换

首先,从原来的网站上,导出 MySQL 数据库备份文件。然后在新服务器上创建一个新的空白数据库,并通过 PHPMyAdmin 或直接在命令行中导入数据库备份文件。

导入数据库后,在 PHPMyAdmin 中选择新数据库,然后在 SQL 查询中执行下面的语句,更新和替换数据库中保留的旧 URL,特别是 wp_options、wp_posts、wp_postmeta 等表。

在执行下面的语句时,把下面语句中的 http://www.oldurl 和 http://www.newurl ,替换为自己的真实 URL 。同时,还要注意您自己的数据库表前缀值是否为默认值 (wp_)。

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

通过 SSH 终端命令行替换

另外,我们还可以通过 ssh 终端登录 MySQL ,然后通过命令行将旧的 URL 更改为新的 URL 。

username@[~/Desktop]: mysql -u root -p databasename
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 892
Server version: 5.5.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') 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 guid = replace(guid, 'http://www.oldurl','http://www.newurl');
Query OK, 0 rows affected (0.02 sec)
Rows matched: 964 Changed: 0 Warnings: 0

mysql> UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
Query OK, 0 rows affected (0.05 sec)
Rows matched: 964 Changed: 0 Warnings: 0

mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
Query OK, 0 rows affected (0.01 sec)
Rows matched: 686 Changed: 0 Warnings: 0

最后,将 WordPress 配置文件(wp-config.php)中的数据库凭据信息进行相应更改。该文件位于 WordPress 根目录下。

define('DB_NAME', 'databasename');

/** MySQL database username */
define('DB_USER', 'username');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/wordpress/replace-old-domain-url-in-wordpress-database/

(0)
牛奇网牛奇网
上一篇 2022年7月25日 下午7:33
下一篇 2022年7月25日 下午8:31

相关推荐

发表回复

登录后才能评论
很多新手不知道如何选择外贸独立站主机,这里推荐一款使用量最大,性价比最高的国外独立站主机Hostinger,立即获取优惠