WordPress 开启多站点(含Apache和Nginx伪静态规则)



远程桌面连接出现身份验证错误 要求的函数不受支持解决方案WordPress 3.0以上的版本支持直接开启多站点模式,这样一来,你可以在一个后台切换多个站点进行管理。

最近打算折腾一个主题演示站,给每个主题使用独立的子站点来搭建演示,如果是Apache环境,配置就比较容易,但是笔者使用的是 Nginx,花了大量的时间测试了N多网络上的伪静态规则,最终找到了完美的解决方法。下面就分享下Linux下 Apache 和 Nginx 两种环境开启多站点的方法。Windows系统环境直接靠边站吧。

开启网络配置

在网站根目录下的 wp-config.php 添加:

<span class="token function">define<span class="token punctuation">(</span></span><span class="token string">'WP_ALLOW_MULTISITE'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

配置多站点模式

刷新网站后台,访问 工具 > 配置网络,根据自己的需要选择“子域名”或者“子目录”模式,然后点击“安装”。
WordPress 开启多站点(含Apache和Nginx伪静态规则)

只有新安装的WordPress,并且绑定了域名,这时候才可以选择“子域名”模式。

如果你选择是的“子域名”,安装后,请将下面的代码添加到网站根目录下的 wp-config.php 下:

<span class="token function">define<span class="token punctuation">(</span></span><span class="token string">'MULTISITE'</span><span class="token punctuation">,</span> <span class="token boolean">true</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">'SUBDOMAIN_INSTALL'</span><span class="token punctuation">,</span> <span class="token boolean">true</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">'DOMAIN_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token string">'yourdomain.com'</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">'PATH_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token string">'/'</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">'SITE_ID_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token number">1</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">'BLOG_ID_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token number">1</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">'MULTISITE'</span><span class="token punctuation">,</span> <span class="token boolean">true</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">'SUBDOMAIN_INSTALL'</span><span class="token punctuation">,</span> <span class="token boolean">false</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">'DOMAIN_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token string">'yourdomain.com'</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">'PATH_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token string">'/'</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">'SITE_ID_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token number">1</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">'BLOG_ID_CURRENT_SITE'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

添加伪静态规则

Apache 规则

如果你的服务器使用的是Aache环境,使用下面的代码替换网站根目录下的 .htaccess 里面的所有代码

子域名模式下的规则代码:

RewriteEngine On
RewriteBase <span class="token operator">/</span>
RewriteRule <span class="token operator">^</span>index\<span class="token punctuation">.</span>php$ <span class="token operator">-</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token comment" spellcheck="true">
# add a trailing slash to /wp-admin
</span>RewriteRule <span class="token operator">^</span>wp<span class="token operator">-</span>admin$ wp<span class="token operator">-</span>admin<span class="token operator">/</span> <span class="token punctuation">[</span>R<span class="token operator">=</span><span class="token number">301</span><span class="token punctuation">,</span>L<span class="token punctuation">]</span>
RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">-</span>f <span class="token punctuation">[</span><span class="token keyword">OR</span><span class="token punctuation">]</span>
RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">-</span>d
RewriteRule <span class="token operator">^</span> <span class="token operator">-</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span>
RewriteRule <span class="token operator">^</span><span class="token punctuation">(</span>wp<span class="token operator">-</span><span class="token punctuation">(</span>content<span class="token operator">|</span>admin<span class="token operator">|</span>includes<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token variable">$1</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span>
RewriteRule <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span>php<span class="token punctuation">)</span>$ <span class="token variable">$1</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span>
RewriteRule <span class="token punctuation">.</span> index<span class="token punctuation">.</span>php <span class="token punctuation">[</span>L<span class="token punctuation">]</span>

子目录模式下的规则代码:

RewriteEngine On
RewriteBase <span class="token operator">/</span>
RewriteRule <span class="token operator">^</span>index\<span class="token punctuation">.</span>php$ <span class="token operator">-</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span> <span class="token comment" spellcheck="true">
# add a trailing slash to /wp-admin
</span>RewriteRule <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span>wp<span class="token operator">-</span>admin$ <span class="token variable">$1wp</span><span class="token operator">-</span>admin<span class="token operator">/</span> <span class="token punctuation">[</span>R<span class="token operator">=</span><span class="token number">301</span><span class="token punctuation">,</span>L<span class="token punctuation">]</span>
RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">-</span>f <span class="token punctuation">[</span><span class="token keyword">OR</span><span class="token punctuation">]</span>
RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">-</span>d
RewriteRule <span class="token operator">^</span> <span class="token operator">-</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span>
RewriteRule <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span>wp<span class="token operator">-</span><span class="token punctuation">(</span>content<span class="token operator">|</span>admin<span class="token operator">|</span>includes<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token variable">$2</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span>
RewriteRule <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span>php<span class="token punctuation">)</span>$ <span class="token variable">$2</span> <span class="token punctuation">[</span>L<span class="token punctuation">]</span>
RewriteRule <span class="token punctuation">.</span> index<span class="token punctuation">.</span>php <span class="token punctuation">[</span>L<span class="token punctuation">]</span>

Nginx 规则

服务器使用的是 军哥 LNMP 1.3 安装包配置的。

假设你的网站为 yourdomain.com,那么你可以编辑这个配置文件:

vi /usr/local/nginx/conf/vhost/yourdomain.com.conf
在最后一个 } 的前面添加下面的代码:

<span class="token comment" spellcheck="true">#ignored: “-” thing used or unknown variable in regex/rew
</span>rewrite <span class="token operator">^</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span>wp<span class="token operator">-</span>admin$ <span class="token operator">/</span><span class="token variable">$1wp</span><span class="token operator">-</span>admin<span class="token operator">/</span> permanent<span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-</span>f <span class="token variable">$request_filename</span><span class="token punctuation">)</span><span class="token punctuation">{</span>	set <span class="token variable">$rule_2</span> <span class="token number">1</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 operator">-</span>d <span class="token variable">$request_filename</span><span class="token punctuation">)</span><span class="token punctuation">{</span>	set <span class="token variable">$rule_2</span> <span class="token number">1</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 variable">$rule_2</span> <span class="token operator">=</span> <span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment" spellcheck="true">
#ignored: “-” thing used or unknown variable in regex/rew
</span><span class="token punctuation">}</span>
rewrite <span class="token operator">^</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span>wp<span class="token operator">-</span><span class="token punctuation">(</span>content<span class="token operator">|</span>admin<span class="token operator">|</span>includes<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">/</span><span class="token variable">$2</span> last<span class="token punctuation">;</span>
rewrite <span class="token operator">^</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span>php<span class="token punctuation">)</span>$ <span class="token operator">/</span><span class="token variable">$2</span> last<span class="token punctuation">;</span>
rewrite <span class="token operator">/</span><span class="token punctuation">.</span> <span class="token operator">/</span>index<span class="token punctuation">.</span>php last<span class="token punctuation">;</span>

以上的规则对于 子域名 和 子目录 模式都正常使用,但是,如果你选择的是 子域名 模式,还需要找到:

server_name yourdomain.com;
添加一个泛域名 *.yourdomain.com,如下:

server_name yourdomain.com *.yourdomain.com;
保存文件,然后重启服务器:

<span class="token operator">/</span>root<span class="token operator">/</span>lnmp restart

然后在你的域名管理那里添加一条泛域名 *.yourdomain.com 解析到主机IP(或者分别解析你即将要使用的子域名到主机IP),这样一来,创建子域名站点时,就可以正常访问了。

附:以下是 Nginx 下最完整的多站点配置代码,实际使用时,请根据自己的实际修改域名及网站目录,同时建议删除含有中文的行:

<span class="token comment" spellcheck="true"># 根据自己的实际修改下面的域名
</span>log_format yourdomain<span class="token punctuation">.</span>com <span class="token string">'$remote_addr - $remote_user [$time_local] "$request" '</span> <span class="token string">'$status $body_bytes_sent "$http_referer" '</span> <span class="token string">'"$http_user_agent" $http_x_forwarded_for'</span><span class="token punctuation">;</span>
server <span class="token punctuation">{</span> listen <span class="token number">80</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> # 根据自己的实际修改下面的域名
</span> server_name yourdomain<span class="token punctuation">.</span>com <span class="token operator">*</span><span class="token punctuation">.</span>yourdomain<span class="token punctuation">.</span>com<span class="token punctuation">;</span> index index<span class="token punctuation">.</span>html index<span class="token punctuation">.</span>htm index<span class="token punctuation">.</span>php <span class="token keyword">default</span><span class="token punctuation">.</span>html <span class="token keyword">default</span><span class="token punctuation">.</span>htm <span class="token keyword">default</span><span class="token punctuation">.</span>php<span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> # 注意修改下面的网站目录
</span> root <span class="token operator">/</span>mnt<span class="token operator">/</span>wwwroot<span class="token operator">/</span>yourdomain<span class="token punctuation">.</span>com<span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> #include wordpress.conf;
</span> <span class="token comment" spellcheck="true"> #以下是 lnmp 1.0 的 wordpress.conf 规则,也就是上一行 include wordpress.conf 里面的
</span> location <span class="token operator">/</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-</span>f <span class="token variable">$request_filename</span><span class="token operator">/</span>index<span class="token punctuation">.</span>html<span class="token punctuation">)</span><span class="token punctuation">{</span> rewrite <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token variable">$1</span><span class="token operator">/</span>index<span class="token punctuation">.</span>html <span class="token keyword">break</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 operator">-</span>f <span class="token variable">$request_filename</span><span class="token operator">/</span>index<span class="token punctuation">.</span>php<span class="token punctuation">)</span><span class="token punctuation">{</span> rewrite <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token variable">$1</span><span class="token operator">/</span>index<span class="token punctuation">.</span>php<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token operator">-</span>f <span class="token variable">$request_filename</span><span class="token punctuation">)</span><span class="token punctuation">{</span> rewrite <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">/</span>index<span class="token punctuation">.</span>php<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true"> # wordpress.conf 规则 结束
</span> location <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>php<span class="token operator">|</span>php5<span class="token punctuation">)</span><span class="token operator">?</span>$ <span class="token punctuation">{</span> try_files <span class="token variable">$uri</span> <span class="token operator">=</span><span class="token number">404</span><span class="token punctuation">;</span> fastcgi_pass unix<span class="token punctuation">:</span><span class="token operator">/</span>tmp<span class="token operator">/</span>php<span class="token operator">-</span>cgi<span class="token punctuation">.</span>sock<span class="token punctuation">;</span> fastcgi_index index<span class="token punctuation">.</span>php<span class="token punctuation">;</span> <span class="token keyword">include</span> fcgi<span class="token punctuation">.</span>conf<span class="token punctuation">;</span> <span class="token punctuation">}</span> location <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>gif<span class="token operator">|</span>jpg<span class="token operator">|</span>jpeg<span class="token operator">|</span>png<span class="token operator">|</span>bmp<span class="token operator">|</span>swf<span class="token punctuation">)</span>$ <span class="token punctuation">{</span> expires 30d<span class="token punctuation">;</span> <span class="token punctuation">}</span> location <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>js<span class="token operator">|</span>css<span class="token punctuation">)</span><span class="token operator">?</span>$ <span class="token punctuation">{</span> expires 12h<span class="token punctuation">;</span> <span class="token punctuation">}</span> access_log <span class="token operator">/</span>home<span class="token operator">/</span>wwwlogs<span class="token operator">/</span>huchangmeng<span class="token punctuation">.</span>com<span class="token punctuation">.</span>log huchangmeng<span class="token punctuation">.</span>com<span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> #ignored: “-” thing used or unknown variable in regex/rew
</span> rewrite <span class="token operator">^</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span>wp<span class="token operator">-</span>admin$ <span class="token operator">/</span><span class="token variable">$1wp</span><span class="token operator">-</span>admin<span class="token operator">/</span> permanent<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-</span>f <span class="token variable">$request_filename</span><span class="token punctuation">)</span><span class="token punctuation">{</span> set <span class="token variable">$rule_2</span> <span class="token number">1</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 operator">-</span>d <span class="token variable">$request_filename</span><span class="token punctuation">)</span><span class="token punctuation">{</span> set <span class="token variable">$rule_2</span> <span class="token number">1</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 variable">$rule_2</span> <span class="token operator">=</span> <span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> #ignored: “-” thing used or unknown variable in regex/rew
</span> <span class="token punctuation">}</span> rewrite <span class="token operator">^</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span>wp<span class="token operator">-</span><span class="token punctuation">(</span>content<span class="token operator">|</span>admin<span class="token operator">|</span>includes<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">/</span><span class="token variable">$2</span> last<span class="token punctuation">;</span> rewrite <span class="token operator">^</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">_0</span><span class="token operator">-</span>9a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span>php<span class="token punctuation">)</span>$ <span class="token operator">/</span><span class="token variable">$2</span> last<span class="token punctuation">;</span> rewrite <span class="token operator">/</span><span class="token punctuation">.</span> <span class="token operator">/</span>index<span class="token punctuation">.</span>php last<span class="token punctuation">;</span> <span class="token punctuation">}</span>

修改固定链接结构

主站点

这里说的主站的,默认就是最初安装时的主域名,比如 yourdomain.com

如果是 子域名 模式,你可以直接在这个主站的的 设置 > 固定链接 直接修改即可。

如果是 子目录 模式,主站点的文章都自动在链接中添加了 /blog/,如果你直接在这个子目录站点的 设置 > 固定链接 设置结构,是无法去除 /blog/ 的,而且在 lnmp 环境下,直接修改会自动添加 index.php。

解决办法是,访问 所有站点,点击主站点的编辑:

WordPress 开启多站点(含Apache和Nginx伪静态规则)

切换到“设置”标签,找到 Permalink Structure ,在这里直接填写 固定链接结构(比如 /%postname%.html),保存即可。这样就可以去除 /bolg/ ,同时不会添加 index.php,非常完美。

子站点

子站点直接在这个站点的后台 设置 > 固定链接 那里设置即可。

到这里,一切基本的设置都完成啦!

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

发表评论