IIS Rewrite配置301 URL重定向技术咨询
IIS Rewrite配置验证:301重定向需求是否满足?
需求说明
需要实现以下301永久重定向规则:
- http://wwww.old.com/News/List → http://wwww.new.com/News/List
- http://wwww.old.com/News/List?page=1&type=2 → http://wwww.new.com/News/List?page=1&type=2
- http://wwww.old.com → http://wwww.new.com
当前IIS Rewrite配置
<rewrite> <rules> <rule name="site2.com" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="http://wwww.old.com" /> </conditions> <action type="Rewrite" url="http://wwww.new.com/{R:0}" /> </rule> </rules> </rewrite>
配置问题分析
当前配置不满足需求,存在三个关键问题:
- 重定向类型错误:用了
Rewrite(服务器内部重写),但需求是对外的301永久重定向,需要把action的type改成Redirect,并添加redirectType="Permanent"明确类型。 - 域名匹配规则错误:
{HTTP_HOST}变量只包含域名(比如wwww.old.com),不包含http://协议头,所以pattern应该写成^wwww\.old\.com$(正则里点号要转义,确保精确匹配)。 - 查询字符串丢失:当前配置不会自动携带原URL的查询参数,需要添加
appendQueryString="true"来保证参数传递。
修正后的配置
<rewrite> <rules> <rule name="Old to New Site 301 Redirect" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^wwww\.old\.com$" /> </conditions> <action type="Redirect" url="http://wwww.new.com/{R:0}" redirectType="Permanent" appendQueryString="true" /> </rule> </rules> </rewrite>
修正后效果验证
- 访问
http://wwww.old.com/News/List会301跳转到http://wwww.new.com/News/List - 访问
http://wwww.old.com/News/List?page=1&type=2会自动携带参数,跳转到http://wwww.new.com/News/List?page=1&type=2 - 访问
http://wwww.old.com会直接跳转到http://wwww.new.com
内容的提问来源于stack exchange,提问作者small pig
相关产品推荐
相关产品推荐

