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" />
注意事项:
需要启用 IIS 的 “HTTP 重定向” 功能
(默认没安装,要在服务器管理器 → 添加角色和功能 → Web 服务器 → HTTP 重定向
勾选)。如果配置后访问报 403 Forbidden,通常是:
没有安装
HTTP Redirect
组件web.config 中
<system.webServer>
节点写错destination
没有写完整(比如缺少协议http://
或https://
)