Contents
1 Introduction
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']);
Note: Yes. $_SERVERI don't know why the $ symbol isn't displaying.

This configuration sets the website's siteurl and home address to the "HTTP_HOST" of the current request. The effect is that WordPress treats all accessed domains as valid siteurls and home addresses, thus removing domain name restrictions. Of course, this method relies on ensuring that all requests to WordPress are legitimate. Therefore, it's best used in conjunction with a reverse proxy or with a way to filter out illegal domain requests to 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_HOSTIt 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 HostPass 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 the function.php file 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.
4. 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.
5 Conclusion
If you only need to change the access domain and don't require access from multiple domains, then directly modifying the database is the easiest and most convenient method. If you need access from multiple domains, you can either use a plugin or modify wp-config.php or functions.php, whichever you prefer. However, remember that modifying wp-config.php or functions.php has the highest priority. This method can be used as a temporary solution if it's inconvenient to directly modify the database or if a plugin malfunctions and causes WordPress to be inaccessible.