You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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>

配置问题分析

当前配置不满足需求,存在三个关键问题:

  1. 重定向类型错误:用了Rewrite(服务器内部重写),但需求是对外的301永久重定向,需要把action的type改成Redirect,并添加redirectType="Permanent"明确类型。
  2. 域名匹配规则错误:{HTTP_HOST}变量只包含域名(比如wwww.old.com),不包含http://协议头,所以pattern应该写成^wwww\.old\.com$(正则里点号要转义,确保精确匹配)。
  3. 查询字符串丢失:当前配置不会自动携带原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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:50:20