iis中跳转网站配置,web.config http重定向设置说明

来源:本站原创 点击数: 发布时间:2025年08月28日

在开始之前确保iis安装了,http重定向模块,否则无法使用以下功能

一、配置位置

web.config 文件中使用 <system.webServer> 节点下的 <httpRedirect> 来配置。
完整示例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>  
<system.webServer>    
<httpRedirect enabled="true" destination="https://www.example.com" httpResponseStatus="Permanent" />  
</system.webServer>
</configuration>

相关参数说明:

  • enabled

    • true:启用重定向

    • false:禁用重定向

  • destination

    • "https://www.example.com/" → 强制跳转到 HTTPS

    • "/newpage.html" → 跳转到站点内的页面

    • 重定向的目标 URL,可以是绝对地址(推荐写完整协议 + 域名),也可以是相对路径。

  • httpResponseStatus
    定义返回给客户端的 HTTP 状态码:

    • Found → 302 临时重定向

    • Permanent → 301 永久重定向

    • Temporary → 307 临时重定向(保持请求方法,比如 POST 不会丢失)

    • PermanentRedirect → 308 永久重定向(保持请求方法)

  • childOnly(可选)

    • true → 只重定向当前文件夹下的请求,不影响子目录

    • false → 包含子目录(默认)

  • exactDestination(可选)

    • true → 不附加原始 URL 路径,直接跳到目标地址

    • false → 保留原始 URL 的子路径并附加到目标地址


二、常见应用场景

1. 强制跳转到 HTTPS

<httpRedirect enabled="true" destination="https://www.example.com" httpResponseStatus="Permanent" />

2. 整站跳转到新域名

<httpRedirect enabled="true" destination="http://newdomain.com" httpResponseStatus="Permanent" />

3. 某个目录跳转到新位置

<httpRedirect enabled="true" destination="/newfolder/" httpResponseStatus="Found" />

注意事项:

  1. 需要启用 IIS 的 “HTTP 重定向” 功能
    (默认没安装,要在 服务器管理器 → 添加角色和功能 → Web 服务器 → HTTP 重定向 勾选)。

  2. 如果配置后访问报 403 Forbidden,通常是:

    • 没有安装 HTTP Redirect 组件

    • web.config 中 <system.webServer> 节点写错

    • destination 没有写完整(比如缺少协议 http://https://