在基于Windows系统的PHP虚拟主机上实现域名的301永久重定向

作者:荒原之梦

操作背景:

当网站在更换或添加域名、进行网址规范化或删除旧页面时,出于对用户使用体验和搜索引擎优化方面的考虑就需要进行301永久重定向。

操作环境:

PHP环境的Windows系统虚拟主机。

操作原理:

本文所介绍的301永久重定向是通过ISAPI_Rewrite这个用于IIS的URL重写组件实现的,关于ISAPI_Rewrite的更多信息可以参见:
1 ISAPI_REWRITE百度百科:Here
2 ISAPI_Rewrite 3 – Apache .htaccess mod_rewrite compatible module for IIS :Here

具体步骤如下:

1 新建一个文本文档,输入以下内容:
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
# Protect httpd.ini and httpd.parse.errors files
# from accessing through HTTP

#将daohanggou.wang重定向到daohanggou.cn
RewriteCond Host: ^daohanggou\.wang$
RewriteRule (.*) http\://daohanggou\.cn$1 [I,RP]

#将www.daohanggou.wang重定向到daohanggou.cn
RewriteCond Host: www\.daohanggou\.wang$
RewriteRule (.*) http\://daohanggou\.cn$1 [I,RP]

#将www.daohanggou.cn重定向到daohanggou.cn
RewriteCond Host: www\.daohanggou\.cn$
RewriteRule (.*) http\://daohanggou\.cn$1 [I,RP]
2 将该文件上传到虚拟主机的web 目录(有的虚拟主机中相应目录的名称为public_html ),重命名为httpd.ini .
之后可以通过http状态查询工具检测301重定向是否成功。
注:上述方法不需要虚拟主机支持伪静态。