英文版本:http://tech.shichen.org/2023/03/15/update-wordpress-url-settings/
在WordPress中,您可以自定义两个主要的URL设置:
WP_HOME
并在数据库中存储为home
。WP_SITEURL
并在数据库中存储为siteurl
。默认情况下,它们都被设置为WordPress安装时的域名。而在大多数情况下这两个URL是相同的,除非您有非常具体的需求并且知道您在做什么,否则不要将它们设置为不同的值。请参考 Giving WordPress Its Own Directory 了解它们的详细用法。
请务必了解,更改这些字段会影响WordPress网站的功能,并可能导致链接失效和404错误。因此,建议在进行更改之前备份您的数据。
当您将网站从一个域名迁移到另一个域名,或将网站从子域名迁移到根域名时。在这些情况下,您都需要更新WordPress中的WordPress Address (URL)和Site Address (URL)以使新的域名生效。
这里我列出三种方法,但只着重讲解第三种。这是因为第一种方法需要您仍然能够访问您的Wordpress网站,而后两种方法做了同样的事情但使用了不同的工具。如果您是Windows用户,建议使用带有图形界面的第二种方法。如果您是Linux用户,建议使用第三种方法,因为它更加直接,并且只需要命令行。
您可以通过WordPress控制面板更新WordPress网站的WordPress Address (URL) 和Site Address (URL):
WordPress Address (URL)
和Site Address (URL)
字段更新为您新的URL。更新home
和siteurl
后,您可能需要清除浏览器缓存和cookie才能看到反映在您网站上的更改。
如果您已经无法访问您的WordPress站点,或者您更喜欢通过数据库更新home
和siteurl
,请执行以下步骤:
wp_options
表格,然后单击将其打开。siteurl
和home
行,然后单击每行旁边的编辑按钮。option_value
字段中将旧的URL替换为新的URL,然后单击Go按钮保存更改。更新home
和siteurl
后,您可能需要清除浏览器缓存和cookie才能看到反映在您网站上的更改。
您还可以通过命令行更新WordPress数据库中的siteurl
和home
值,步骤如下:
mysql
包:sudo dnf install -y mysql # Take Red Hat Linux as an example
mysql -u DB_USERNAME -p
如果您从另一台主机连接mysql,您可以使用以下命令来指定提供数据库服务的主机:
mysql -h DB_HOST -u DB_USERNAME -p
并在出现提示时输入您的密码,然后按回车键。
show databases
use DB_NAME
wp_options
表中siteurl
和home
的当前值:SELECT option_value FROM wp_options WHERE option_name = 'siteurl';
SELECT option_value FROM wp_options WHERE option_name = 'home';
wp_options
表中siteurl
和home
的值: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';
siteurl
和home
的值已经更新。exit
并按回车键退出MySQL命令行界面。更新home
和siteurl
后,您可能需要清除浏览器缓存和cookie才能看到反映在您网站上的更改。
下面是一个通过命令行更新siteurl
和home
的例子。它使用基于 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 distributionCopyright (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 -ADatabase changedmysql> 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: 0mysql> 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: 0mysql> 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
上一篇:Java集合源码分析