昨天把博客搬了个家,本来用的是阿里云虚拟主机免费版,现在换成ECS学生版,装的是winServer2012系统,配置环境的时候就弄得很头痛。不过最终还是弄好了。
搬家的过程还是比较痛苦的,遇到数据库导入错误,网上说是跨版本的的原因,无解,就在我快要放弃导入数据库的时候,发上直接导入.sql文件能成功,导入.zip压缩文件就出现错误11.
还是进入今天的主题吧
准备工作
rewrite_x64_zh-CN.msi 这是微软提供的伪静态程序程序(这里我的系统是64位的),可自行百度下载
这里哀差闷也提供一个网盘链接:http://pan.baidu.com/s/1mgngE0K 密码:sji2
安装rewrite
这步没什么说的,一直点下一步就行了
双击运行“rewrite_x64_zh-CN.msi”
接受许可条款,点击“安装”
安装中
配置web.config
在网站的根目录新建一个web.config文件,如果已经存在直接打开就行了
把下面的代码copy进去,要覆盖掉原有代码:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="category"> <match url="category/?(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="/index.php?category_name={R:1}" appendQueryString="false" logRewrittenUrl="false" /> </rule> <rule name="tags"> <match url="tag/?(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="index.php?tag={R:1}" /> </rule> <rule name="Main Rule" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php/{R:0}" /> </rule> <rule name="wordpress" patternSyntax="Wildcard"> <match url="*" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <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>
使用%postname%之类的固定链接出现404的解决方法
之前在虚拟主机的时候一直用的是Http://www.icharm.me?%postname%的固定链接,换到IIS后,发现所有带有中文字符的连接都会出现404错误。但是换成默认的链接形式都可以正确访问,那说明URL重写没有问题,但是为什么带有中文字符的链接会出现404呢?
百度谷歌了一番知道是什么原因了。因为WP的编码为utf-8,而这篇文章的URL中postname部分编码为gbk。然后WP取得文章postname部分后,通过它来查找文章那肯定找不到咯,编码都不同!
所以要对取得postname部分的变量进行一个编码的转换。
1.打开wp-include/class-wp.php(老版本wp是在wp-include/classes.php文件中)
2.找到下面的代码段
if ( ! empty($rewrite) ) { // If we match a rewrite rule, this will be cleared. $error = '404'; $this->did_permalink = true; $pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; list( $pathinfo ) = explode( '?', $pathinfo ); $pathinfo = str_replace( "%", "%25", $pathinfo ); list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] ); $self = $_SERVER['PHP_SELF']; $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
3.对$_SERVER['PATH_INFO']和$_SERVER['REQUEST_URI']进行编码转换,
把上面的$_SERVER['PATH_INFO'] 改为 mb_convert_encoding($_SERVER['PATH_INFO'], "utf-8", "GBK")
$_SERVER['REQUEST_URI'] 改为 mb_convert_encoding($_SERVER['REQUEST_URI'], "utf-8", "GBK")
if ( ! empty($rewrite) ) { // If we match a rewrite rule, this will be cleared. $error = '404'; $this->did_permalink = true; $pathinfo = isset( $_SERVER['PATH_INFO'] ) ? mb_convert_encoding($_SERVER['PATH_INFO'], "utf-8", "GBK") : ''; list( $pathinfo ) = explode( '?', $pathinfo ); $pathinfo = str_replace( "%", "%25", $pathinfo ); list( $req_uri ) = explode( '?', mb_convert_encoding($_SERVER['REQUEST_URI'], "utf-8", "GBK") ); $self = $_SERVER['PHP_SELF']; $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
Comments | NOTHING