WordPress多域名绑定

第一、实现任意域名的访问

define('WP_SITEURL', 'http://'.$_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://'.$_SERVER['HTTP_HOST']);

我们在WordPress程序根目录wp-config.php文件中加上上面代码,这样只要是解析进来的域名都可以打开且不会看到串联到其他域名。

第二、限制特定域名访问

$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']);
}

define('WP_CONTENT_URL', '/wp-content');

如果是启用了SSL的站点,需要把上面的http改成https

.htaccess绑定域名到子目录

在网站根目录下添加.htaccess


RewriteEngine On
RewriteCond %{HTTP_HOST} ^(blog\.)?xiaokyun\.com$
RewriteRule ^blog/(.*)$ http://blog.xiaokyun.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.xiaokyun\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]

在子目录下添加.htaccess

RewriteEngine On
RewriteBase /blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]