不少朋友总是询问 WordPress 如何添加伪静态规则,今天幻杀就总结一下 IIS6/IIS7.5/Apache/Nginx/win2003/win2008四种环境下的伪静态规则,希望对大家有所帮助。其中IIS7.5的规则是目前最完美的哦
明明是六种为什么要说是四种呢?其实windows的主机在2003时为IIS6,而2008以后升级为了IIS7.5 所以IIS6的规则在IIS7.5不通用哦,其实我以前也很纠结的。弄了半天规则才发现,悲催了
检测主机是否支持伪静态的方法:在WP后台 > 设置 > 固定链接,设置为 非默认带?的那种结构,然后访问任何一篇文章,如果出现 404 错误,说明你的主机当前不支持 WordPress 伪静态。
IIS 环境是 Windows 主机常用的服务器环境,但网上很多规则其实并不完美,很多地方依旧有些无法设置的地方,但这个却可以,新建两个 txt 文件,将下面的代码分别添加到文件中:
第一个 web.config
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ChineseURL" stopProcessing="true">
<match url="^(tag|category)/(.*)$" />
<action type="Rewrite" url="ihuanurl.php"/>
</rule>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
第二个辅助文件 ihuanurl.php:
<?php
// IIS Mod-Rewrite
if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
}
// IIS Isapi_Rewrite
else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
}
else
{
// Use ORIG_PATH_INFO if there is no PATH_INFO
if ( !isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO']) )
$_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
// Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
if ( isset($_SERVER['PATH_INFO']) ) {
if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
else
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
}
// Append the query string if it exists and isn't null
if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
}
require("index.php");
?>
然后保存文件,上传到WordPress站点的根目录即可。
IIS 环境是 Windows 主机常用的服务器环境,新建一个 txt 文件,将下面的代码添加到文件中:
[ISAPI_Rewrite] # Defend your computer from some worm attacks #RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O] # 3600 = 1 hour CacheClockRate 3600 RepeatLimit 32 # Protect httpd.ini and httpd.parse.errors files # from accessing through HTTP # Rules to ensure that normal content gets through RewriteRule /tag/(.*) /index\.php\?tag=$1 RewriteRule /software-files/(.*) /software-files/$1 [L] RewriteRule /images/(.*) /images/$1 [L] RewriteRule /sitemap.xml /sitemap.xml [L] RewriteRule /favicon.ico /favicon.ico [L] # For file-based wordpress content (i.e. theme), admin, etc. RewriteRule /wp-(.*) /wp-$1 [L] # For normal wordpress content, via index.php RewriteRule ^/$ /index.php [L] RewriteRule /(.*) /index.php/$1 [L]
然后另存为 httpd.ini 文件,上传到WordPress站点的根目录即可。
Apache是 Linux 主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境。新建一个 htaccess.txt 文件,添加下面的代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
然后上传到 WordPress 站点的根目录,重命名为 .htaccess 即可
Nginx环境一般是Linux 主机 VPS或服务器用户用的比较多,这些用户一般都会自己配置Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.conf(不同人配置的不一样),在 server { } 大括号里面添加下面的代码:
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
保存,重启 Nginx 即可。
最后来个结尾:你们会问我我为什么知道IIS7.5的规则,其实以前我就是用的win2008的机子,但是用来做博客的话,win主机真的不大适合,绝大部分运营商都会禁止smtp的,而且win主机运行php实在是太慢了,推荐在win的主机上安装阿帕奇之后再用,真的,我不想深受其害了
Pretty section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts.
Any way I will be subscribing to your augment and even I achievement you access consistently rapidly.
2018年7月23日 12:12Hi there, I discovered your web site by the use of Google at the same time
as looking for a similar matter, your website came up,
it appears to be like good. I have bookmarked it in my google bookmarks.
Hello there, just became alert to your blog thru Google, and found that it is
2018年7月23日 12:10truly informative. I’m gonna watch out for brussels.
I’ll appreciate for those who continue this in future.
Lots of other people shall be benefited from your writing.
Cheers!
Hello, I enjoy reading all of your post. I wanted
2018年7月23日 11:56to write a little comment to support you.
Nice weblog right here! Additionally your website loads
up very fast! What web host are you the use of?
Can I am getting your affiliate hyperlink to your host?
2018年7月17日 16:42I want my web site loaded up as quickly as yours lol
曾经被 Nginx 所困,后来放弃了!
2017年4月4日 09:37nginx挺好用
2017年4月15日 14:19httpd.conf没有权限修改这个文件怎么办?
2017年1月29日 14:42你是虚机还是?vps?
2017年1月30日 15:07你好,我的伪静态去掉category就变成了404是怎么回事呢
2015年9月19日 17:34这是缓存的缘故,你是用的插件吧,我站内应该有相关文章
2015年9月20日 15:37我试了各种方法都不行啊,已经开启域名重写,.htaccess也上传了,还会是什么别的原因吗。
2015年5月8日 09:20行了吗?
2015年5月8日 19:27我QQ发你的能用了?
2015年5月8日 19:29不行。。
2015年5月12日 09:30不会吧……好奇葩,到底安装的什么环境?
2015年5月12日 12:56阿里云服务器,Ubuntu下装的lamp。。
2015年5月12日 21:17博客支持伪静态最好的就是Linux主机,win主机配置太麻烦了。。。。【倡萌博客】0门槛T楼 :杜蕾斯、WIFI、蓝牙耳机免费领。
2014年11月2日 14:26嗯,win主机太坑爹
2014年11月2日 15:22所以选主机要看做什么类型的网站
2014年11月2日 15:47嗯,深有感触啊
2014年11月2日 15:49
2014年11月2日 15:50挺好,当初要是看到这个方法就好了,现在改规则不合适吧
2014年11月1日 18:59你的好像没有伪静态吧
2014年11月1日 20:09没有,不会呢
2014年11月1日 20:30上传下规则,后台一改就OK了
2014年11月1日 21:19对原有的收录影响大吗
2014年11月2日 11:10只要原链接不是效就好了,百度没问题
2014年11月2日 15:23我还是缓缓
2014年11月2日 15:28哈哈,刚开始的时候直接伪静态最好了
2014年11月2日 15:34