Excellent software and practical tutorials
Modify the wp-config.php file to implement WordPress binding multiple domain names and access
A website is available for useMultiple domain namesof.WordPressAfter a website is bound to multiple domain names, except for the main domain name, other domain name access will have js cross-domain errors. How to solve the js cross-domain error? This requires WordPress to support binding multiple domain names.
How to solve the js cross-domain error after binding multiple domain names in WordPress?
WordPress binds multiple domain namesHow to support multiple domain name access?wp-config.php file, WordPress can support access to multiple domain names. Adding the corresponding code in the file allows multiple bound domain names to access the website normally, avoiding js cross-domain errors. By settingSpecific domain name accessOr cancel the domain name binding restriction, and flexibly configure it according to needs. At the same time, you also need to pay attention to the address of static resources to ensure that the access to static files such as pictures can also adapt to multiple domain names. In this way, you can realize the need to bind WordPress to multiple domain names and support access to multiple domain names.
Webmasters who have used WordPress should know that when WordPress is installed, it will bind to the current domain name by default. It will bind other domain names later, but the page links will still be the domain name at the time of installation. So today I will share with you how to bind WordPress to multiple domain names or cancel the domain name binding restriction.
Find the wp-config.php file in the root directory of the website and find the code in the file require_once(ABSPATH . 'wp-settings.php');
Add the following code above it
(It must be above this code, otherwise the CSS styles, images, etc. cannot be read.)
It is not difficult to bind multiple domain names to WordPress and access them independently. We only need to modify the wp-config.php file, find the "wp-config.php" file in the root directory of the site, and then add any of the following codes according to your needs.
How to bind multiple domain names to WordPress in the wp-config.php file?
Achieve access to any domain name
Note: Add the following code to the wp-config.php file to ensure that all bound and resolved domain names can access the blog normally without being connected to other domain names.
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
or
$home = 'http://'.$_SERVER['HTTP_HOST']; $siteurl = 'http://'.$_SERVER['HTTP_HOST']; define('WP_HOME', $home); define('WP_SITEURL', $siteurl);
PHP $_SERVER['HTTP_HOST']
Used to obtain the source domain name. In this way, WP_HOME
and WP_SITEURL
Variable assignment to implement WordPress multi-domain binding.
Implement https access to any domain name
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
or
$home = 'https://'.$_SERVER['HTTP_HOST']; $siteurl = 'https://'.$_SERVER['HTTP_HOST']; define('WP_HOME', $home); define('WP_SITEURL', $siteurl);
How to restrict specific domains from accessing WordPress websites? Restrict access to specific domains
You can also specify that only certain domain names can be accessed. $domain = array("www.a.com", "www.b.com", "www.c.com");
Change the domain name in the brackets to the domain name you want to bind. This way, you can set the domain names you added to be accessible normally, while other domain names that have not been added cannot be accessed.
Set the website domain name to the currently visited domain name, that is, cancel the domain name binding. If you do not need any domain name to be accessible
$domain = array("www.a.com", "www.b.com", "www.c.com"); if(in_array($_SERVER['HTTP_HOST'], $domain)){ define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']); }
Just put the specified domain name in the $domain array.
How to modify the static resource address to accommodate multiple bound domain names?
Your website can already achieve multi-domain access, but there is still a problem, that is, static resources. When the pictures uploaded to WordPress are inserted into the article, the address is fixed. After changing the domain name, the domain name of the picture will not be changed, so you also need to modify the static file address. Use the following code to solve it:
define( 'WP_CONTENT_URL', '//' . $_SERVER['HTTP_HOST'] . '/wp-content');
Add this code below the above code. OK, now your website has perfectly broken through the restriction of domain name binding.