Contents
Preface
When we use WordPress to build our own personal blog, we often encounter the problem that we need to use multiple ways to access WordPress. For example, in the intranet, I want to use the intranet address 192.168.xx to access it, and in the extranet, I want to use the domain name bbs1.example.com to access it, or I also want to use bbs2.example.com to access it. This requirement is actually very normal and common, but it cannot be achieved in the default WordPress, because by default WordPress will bind to the access address used during initialization (it may be an IP address or a domain name).
In fact, if WordPress does not do the SSL decryption itself (the usual practice now is to perform SSL decryption on the reverse proxy if there is a reverse proxy), then WordPress locking an access address is not very meaningful from a security perspective, because the access domain name of the request that can reach WordPress through the reverse proxy must be correct.
In order to enable WordPress to support multi-domain access, there are generally three ways to implement it.
Method 1: Modify wp-config.php
The domain name bound when initializing WordPress is stored in the database. The specific location is in the table wp_options in the library corresponding to WordPress:
Therefore, many problems that lead to inaccessibility of WordPress after changing the domain name or IP address or http to https can be fixed by directly modifying the address in the database, where home and siteurl correspond to the WordPress address and site address respectively, as shown in the following figure:
Note: If you want to modify the database directly, you can use the command line or various database clients (DBeaver, phpadmin, etc.) to modify it directly. I won’t go into details here. There are many articles on the Internet. This is not the point I want to talk about today.
The method of modifying wp-config.php that we are going to talk about in this section uses the principle that in WordPress, for the same setting data, constants have higher priority than variables defined in the database. WordPress has two constants, WP_HOME and WP_SITEURL, which correspond to home and siteurl in the database respectively. Therefore, as long as these two constants are set in wp-config.php, they will take precedence over the settings in the database. There are several modification ideas as follows.
1. Fully open without any restrictions
In the wp-config.php filedefine('WP_DEBUG', false);
Add the following code afterwards:
define('WP_SITEURL', 'http://' . _SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' ._SERVER['HTTP_HOST']);
这样配置就是设置网站的siteurl和home为当前请求的”HTTP_HOST”,效果就是不管使用什么域名来访问都被wordpress当成合法的siteurl和home,也就没有了访问域名的限制。当然,采用这种方式的前提是能够达到wordpress的请求都是正常请求,所以这种方式最好是和反向代理配合使用,或者能够有办法过滤达到wordpress上的非法域名请求。
2. Directly limit the domain name that can access WordPress
In the wp-config.php filedefine('WP_DEBUG', false);
Add the following code afterwards:
$domain = array("bbs1.example.com", "bbs2.example.com", "bbs3.example.com"); if(in_array($_SERVER['HTTP_HOST'], $domain)){ define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']); }
The effect of this configuration is to access the requestHTTP_HOST
It can only be the domain name set in $domain. In the above figure, it is bbs1.example.com, bbs2.example.com, and bbs3.example.com.
Of course, you can also limit access to a domain name, taking bbs1.example.com as an example:
define('WP_SITEURL', 'http://bbs1.example.com'); define('WP_HOME', 'http://bbs1.example.com');
If you want to use this method and deploy it with a reverse proxy, you need to set up the reverse proxy correctly.proxy_set_header Host
Pass the correct HOST.
Note: Modifying wp-config.php will cause the siteurl and home fields in the backend settings interface to become gray and unable to be modified:
However, it does not affect normal use, but it may look a bit uncomfortable.
In addition: The above method is invalid for static resources in WordPress, such as uploaded attachments, pictures, etc. If you want to modify the address of such static resources, you also need to insert the following code:
define( 'WP_CONTENT_URL', '//' . $_SERVER['HTTP_HOST'] . '/wp-content');
Method 2: Modify function.php of the current theme
Method 2 is actually the same as Method 1, both of which use the method of setting the two constants WP_HOME and WP_SITEURL, but Method 1 is set in wp-config.php, while Method 2 is set in function.php of the current theme. The settings are the same, so I will not repeat them here.
Method 3: Plugin
There should be many plug-ins to achieve multi-domain access. I used Multiple Domain before:
It is very simple to use. Just add the domain name to access, as shown below:
This plugin is actually very good, but it hasn’t been updated for a long time, which made me, who has obsessive-compulsive disorder, very unhappy, so I later switched to modifying wp-config.php.
Note: Using the plugin in this way will not cause the siteurl and home options in the background settings interface to become gray.
Summarize
In fact, if you don’t need multi-domain access, but just need to modify the access domain name, then I think directly modifying the database is the most worry-free and labor-saving way; if you need multi-domain access, you can directly use the plug-in method to achieve it, or you can modify wp-config.php or function.php to achieve it, it depends on your preference.
However, the most important point is that modifying wp-config.php or function.php has the highest priority. If it is inconvenient to modify the database directly, or if there is a problem with the plug-in that causes WordPress to be inaccessible normally, this method can be used as an emergency measure.