Win主机二级目录下WordPress博客程序设置固定链接实现真正伪静态的方法

简子 2021-11-28 3,749 11/28

wordpress

之前小简介绍过关于win主机下WordPress实现完美伪静态的方法,通过这个方法可以完美实现在win主机下WordPress的伪静态!

有的做公司的主页网站,只能把WordPress放到二级目录(子目录)里面,比如:http://www.baidu.com/blog,类似这样的我们就面临一个问题了,WordPress程序放到二级目录,文章页面打不开了,那是因为以前的rewrite伪静态规则不能用了,以前的Rewrite规则是实现根目录下WordPress的伪静态!

经过小简的测试,以下代码可以实现Win主机二级目录中WordPress博客程序实现伪静态!

把下面代码,保存为httpd.ini,上传到你网站的根目录(其中/blog 替换为你的二级目录(子目录)名)

[ISAPI_Rewrite]
# 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 /blog/sitemap.xml /blog/sitemap.xml [L]
RewriteRule /blog/favicon.ico /blog/favicon.ico [L]
# For tag
RewriteRule /blog/tag/(.*)/page/(\d+)$ /blog/index\.php\?tag=$1&paged=$2
RewriteRule /blog/tag/(.+)$ /blog/index\.php\?tag=$1
# For category
RewriteRule /blog/category/(.*)/page/(\d+)$ /blog/index\.php\?category_name=$1&paged=$2
RewriteRule /blog/category/(.*) /blog/index\.php\?category_name=$1
# For file-based wordpress content (i.e. theme), admin, etc.
RewriteRule /blog/wp-(.*) /blog/wp-$1 [L]
# For normal wordpress content, via index.php
RewriteRule ^/blog/$ /blog/index.php [L]
RewriteRule /blog/(.*) /blog/index.php/$1 [L]
#For page
RewriteRule /blog/page/(.*)/?s=(.*) /blog/index\.php\?s=$2&paged=$1
RewriteRule /blog/page/(.*) /blog/index\.php\?paged=$1

有的win主机不支持Rewrite组件,那么httpd.ini文件就没用了,该怎么办呢?

其实还是有方法可以实现的, 少数虚拟主机不支持Rewrite组件的,或者你以前不是通过Rewrite规则实现伪静态的, 你可以尝试这样一种方法,通过主机提供的自定义404页面,同样可以实现!

把下面代码保存为“404.php”上传到网站根目录(其中blog\ 替换为你的二级目录(子目录)名)

header('Status : 200 OK');
$ori_qs = $_SERVER['QUERY_STRING'];
$pattern = '/[^;]+;[^:]+://[^/]+(/[^?]*)(?:?(.*))?/i';
preg_match($pattern,$ori_qs,$matches);
$_SERVER['PATH_INFO']   = $matches[1].'?'.$matches[2];
$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
$query_args= explode('&',$matches[2]);
unset($_GET);
foreach ($query_args as $arg)
{
$the_arg = explode('=',$arg);
$_GET[$the_arg[0]] = $the_arg[1];
}
include('blog\index.php');
?>

登录主机商的网站管理后台!找到“自定义出错页”进行设置

还有一个经常会被忽略的,wordpress目录里的index.php文件,这个文件有一句

require('.\wp-blog-header.php');

我们要把它改成

require('wp-blog-header.php');

去掉“.\ ”

这是很多使用404自定义页面实现伪静态最终失败的原因,就是没有修改index.php这个文件!

现在我们可以登录网站后台设置自己喜欢的伪静态url格式

以上方法仅在win IIS6主机测试,其他版本的请自行测试!

- THE END -

简子

12月16日22:05

最后修改:2022年12月16日
0

非特殊说明,本博所有文章均为博主原创。

共有 0 条评论