英文版本:http://tech.shichen.org/2023/03/15/update-wordpress-url-settings/

WordPress中有哪些URL设置?

在WordPress中,您可以自定义两个主要的URL设置:

  1. WordPress Address (URL):此字段指定WordPress文件所在的URL。它也被称为WP_HOME并在数据库中存储为home
  2. Site Address (URL):此字段指定访问者用来访问WordPress站点时的URL。它也称为WP_SITEURL并在数据库中存储为siteurl

默认情况下,它们都被设置为WordPress安装时的域名。而在大多数情况下这两个URL是相同的,除非您有非常具体的需求并且知道您在做什么,否则不要将它们设置为不同的值。请参考 Giving WordPress Its Own Directory 了解它们的详细用法。

请务必了解,更改这些字段会影响WordPress网站的功能,并可能导致链接失效和404错误。因此,建议在进行更改之前备份您的数据。

我什么时候需要修改它们?

当您将网站从一个域名迁移到另一个域名,或将网站从子域名迁移到根域名时。在这些情况下,您都需要更新WordPress中的WordPress Address (URL)和Site Address (URL)以使新的域名生效。

更改URL设置都有哪些方法?

这里我列出三种方法,但只着重讲解第三种。这是因为第一种方法需要您仍然能够访问您的Wordpress网站,而后两种方法做了同样的事情但使用了不同的工具。如果您是Windows用户,建议使用带有图形界面的第二种方法。如果您是Linux用户,建议使用第三种方法,因为它更加直接,并且只需要命令行。

方法一:通过WordPress控制面板

您可以通过WordPress控制面板更新WordPress网站的WordPress Address (URL) 和Site Address (URL):

  1. 以管理员身份登录到您的WordPress控制面板。
  2. 单击左侧菜单中的“Settings”,然后单击“General”。
  3. WordPress Address (URL)Site Address (URL)字段更新为您新的URL。
  4. 单击页面底部的“Save Changes”按钮。

更新homesiteurl后,您可能需要清除浏览器缓存和cookie才能看到反映在您网站上的更改。

方法二:通过数据库管理工具

如果您已经无法访问您的WordPress站点,或者您更喜欢通过数据库更新homesiteurl,请执行以下步骤:

  1. 使用phpMyAdmin或任何其他数据库管理工具登录到您的MySQL数据库。
  2. 选择您WordPress所使用的数据库。
  3. 在表格列表中找到wp_options表格,然后单击将其打开。
  4. 查找siteurlhome行,然后单击每行旁边的编辑按钮。
  5. option_value字段中将旧的URL替换为新的URL,然后单击Go按钮保存更改。
  6. 退出数据库管理工具。

更新homesiteurl后,您可能需要清除浏览器缓存和cookie才能看到反映在您网站上的更改。

方法三:通过命令行

您还可以通过命令行更新WordPress数据库中的siteurlhome值,步骤如下:

  1. 通过SSH或其他命令行界面登录到您的服务器。
  2. 运行以下命令安装mysql包:
sudo dnf install -y mysql		# Take Red Hat Linux as an example
  1. 键入以下命令打开MySQL命令行界面:
mysql -u DB_USERNAME -p

如果您从另一台主机连接mysql,您可以使用以下命令来指定提供数据库服务的主机:

mysql -h DB_HOST -u DB_USERNAME -p

并在出现提示时输入您的密码,然后按回车键。

  1. 通过键入以下内容选择您WordPress所使用的数据库:
show databases
use DB_NAME
  1. 运行以下命令查看wp_options表中siteurlhome的当前值:
SELECT option_value FROM wp_options WHERE option_name = 'siteurl';
SELECT option_value FROM wp_options WHERE option_name = 'home';
  1. 运行以下命令更新wp_options表中siteurlhome的值:
UPDATE wp_options SET option_value='http://newsiteurl.com' WHERE option_name='siteurl';
UPDATE wp_options SET option_value='http://newhome.com' WHERE option_name='home';
  1. 运行第 5 步中的命令以验证siteurlhome的值已经更新。
  2. 通过键入exit并按回车键退出MySQL命令行界面。

更新homesiteurl后,您可能需要清除浏览器缓存和cookie才能看到反映在您网站上的更改。

通过命令行更新URL设置的例子

下面是一个通过命令行更新siteurlhome的例子。它使用基于 Quickstart: Compose and WordPress 的凭据和名称从容器网络内部的另一个容器连接到Wordpress数据库。

[root@c13518a547fb ~]# dnf install -y mysql
......
[root@c13518a547fb ~]# mysql -h db -u wordpress -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.5.5-10.6.4-MariaDB-1:10.6.4+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+
2 rows in set (0.02 sec)

mysql> use wordpress
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> SELECT option_value FROM wp_options WHERE option_name = 'siteurl';
+------------------------------+
| option_value                 |
+------------------------------+
| http://apps.shichen.org:8080 |
+------------------------------+
1 row in set (0.01 sec)

mysql> SELECT option_value FROM wp_options WHERE option_name = 'home';
+------------------------------+
| option_value                 |
+------------------------------+
| http://apps.shichen.org:8080 |
+------------------------------+
1 row in set (0.00 sec)

mysql> UPDATE wp_options SET option_value='http://tech.shichen.org' WHERE option_name='siteurl';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> UPDATE wp_options SET option_value='http://tech.shichen.org' WHERE option_name='home';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT option_value FROM wp_options WHERE option_name = 'siteurl';
+-------------------------+
| option_value            |
+-------------------------+
| http://tech.shichen.org |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT option_value FROM wp_options WHERE option_name = 'home';
+-------------------------+
| option_value            |
+-------------------------+
| http://tech.shichen.org |
+-------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye