一个WordPress如何绑定多个域名教程



使用过WordPress的站长应该都清楚,WordPress在安装的时候会默认绑定当前的域名,后续绑定其他的域名但是页面的链接也还是安装时候的域名。那么今天给大家分享一下WordPress绑定多个域名或者取消域名绑定限制的方法。

1. 打开网站根目录下的wp-config.php,在define(‘WP_DEBUG’, false);后面添加下面内容:

<span class="token function">define<span class="token punctuation">(</span></span><span class="token string">'WP_SITEURL'</span><span class="token punctuation">,</span> <span class="token string">'http://'</span> <span class="token punctuation">.</span> <span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token string">'HTTP_HOST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">define<span class="token punctuation">(</span></span><span class="token string">'WP_HOME'</span><span class="token punctuation">,</span> <span class="token string">'http://'</span> <span class="token punctuation">.</span> <span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token string">'HTTP_HOST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

这两句的意思就是设置网站域名为当前访问的域名,也就是取消了域名的绑定,如果不需要任意域名都能访问,只是几个域名的话,可以这样:

<span class="token variable">$domain</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string">"www.a.com"</span><span class="token punctuation">,</span> <span class="token string">"www.b.com"</span><span class="token punctuation">,</span> <span class="token string">"www.c.com"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">in_array<span class="token punctuation">(</span></span><span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token string">'HTTP_HOST'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$domain</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">define<span class="token punctuation">(</span></span><span class="token string">'WP_SITEURL'</span><span class="token punctuation">,</span> <span class="token string">'http://'</span> <span class="token punctuation">.</span> <span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token string">'HTTP_HOST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">define<span class="token punctuation">(</span></span><span class="token string">'WP_HOME'</span><span class="token punctuation">,</span> <span class="token string">'http://'</span> <span class="token punctuation">.</span> <span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token string">'HTTP_HOST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

把指定的域名放在$domain数组里面即可。

注意:

如果是https,请修改代码里面的http://为https://;
如果网站安装在二级目录,则将’http://’ . $_SERVER[‘HTTP_HOST’]修改为’http://’ . $_SERVER[‘HTTP_HOST’].’/对应目录名’
2. 在完成上面的工作以后,你的网站已经可以实现多域名访问了,但是还是有一个问题,那就是静态资源,在wordpress上传的图片插入文章里面,地址是固定的,修改域名以后,并不会修改图片的域名,所以还需要修改静态文件地址,使用以下代码可以解决:

<span class="token function">define<span class="token punctuation">(</span></span> <span class="token string">'WP_CONTENT_URL'</span><span class="token punctuation">,</span> <span class="token string">'/wp-content'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

把这段代码加入上面的代码下面即可。

OK,到这里你的网站已经完美的突破了域名绑定的限制了。

本文由 冰风BOKE 作者:冰风 发表,其版权均为 冰风BOKE 所有,文章内容系作者个人观点,不代表 冰风BOKE 对观点赞同或支持。如需转载,请注明文章来源。
0

发表评论