WordPress的伪静态规则是根据服务器环境来设置的,不同的PHP环境有不同的伪静态设置方法,常见的PHP环境有 Apache和Nginx ,以下分别就这两种环境做伪静态设置。
aHe使用的是Nginx,设置了如下rewrite规则:
1 2 3 4 5 6 |
location / { try_files $uri $uri/ /index.php?$args; } rewrite /wp-admin$ $scheme://$host$uri/ permanent; |
同时在 WordPress后台的设置——固定链接 页面,选择自定义结构,输入 /%postname%.html ,就能得到 http://yourdomain.com/xxx.html 的静态页面,一下子干净多了。
另外,在发表文章时,标题下面的固定链接处输入与文章内容相关的英文名称,更利于被搜索引擎索引。
Apache规则:
首先要开启apache的url_rewrite模块(一般默认都是开启的),也就是在httpd.conf中去掉这句话的注释LoadModule rewrite_module modules/mod_rewrite.so,httpd.conf中找到AllowOverride,把AllowOverride None修改成AllowOverride all
网站根目录下要有 .htaccess
文件,然后将下面的代码复制进去。
1 2 3 4 5 6 7 8 |
<span class="tag"><ifmodule</span> <span class="atn">mod_rewrite</span><span class="pln">.</span><span class="atn">c</span><span class="tag">></span><span class="pln"> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </span><span class="tag"></ifmodule></span> |
WordPress在Apache环境下二级目录建站伪静态操作方式同上。
Nginx规则:
操作方法:以下代码加入到网站的配置文件 xxxx.conf 中的 server{} 中。
根目录下WordPress的伪静态规则:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="pln">location </span><span class="pun">/</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(-</span><span class="pln">f $request_filename</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">html</span><span class="pun">){</span><span class="pln"> rewrite </span><span class="pun">(.*)</span><span class="pln"> $1</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">html </span><span class="kwd">break</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">if</span> <span class="pun">(-</span><span class="pln">f $request_filename</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">){</span><span class="pln"> rewrite </span><span class="pun">(.*)</span><span class="pln"> $1</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">if</span> <span class="pun">(!-</span><span class="pln">f $request_filename</span><span class="pun">){</span><span class="pln"> rewrite </span><span class="pun">(.*)</span> <span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span> <span class="pun">}</span> <span class="pun">}</span> |
二级目录下WordPress的伪静态规则:
注意将以下代码中的“二级目录名”换成自己的真实二级目录名。
1 2 3 4 5 6 7 8 9 10 11 |
<span class="pln">location </span><span class="pun">/二级目录名/</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(-</span><span class="pln">f $request_filename</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">html</span><span class="pun">){</span><span class="pln"> rewrite </span><span class="pun">(.*)</span><span class="pln"> $1</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">html </span><span class="kwd">break</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">if</span> <span class="pun">(-</span><span class="pln">f $request_filename</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">){</span><span class="pln"> rewrite </span><span class="pun">(.*)</span><span class="pln"> $1</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">if</span> <span class="pun">(!-</span><span class="pln">f $request_filename</span><span class="pun">){</span><span class="pln"> rewrite </span><span class="pun">(.*)</span> <span class="pun">/二级目录名/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span> <span class="pun">}</span> <span class="pun">}</span> |