在 Nginx 中,跳转和伪静态通过 rewrite、try_files 实现;在 Apache 中通过 .htaccess。在 Windows 的 IIS 上,对应的功能由 URL Rewrite(URL 重写)模块 提供,规则写在网站根目录的 web.config 文件中。IIS 的安装见 Windows 安装 IIS。
第一步:安装 URL Rewrite 模块
URL Rewrite 不是 IIS 的内置功能,需要从微软官方网站下载「URL Rewrite Module 2.1」安装包安装。安装后,在 IIS 管理器中选中网站,能看到「URL 重写」图标。
可以通过图形界面添加规则,也可以直接编辑 web.config。本文以 web.config 为主,方便复制和版本管理。
规则的基本结构
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="规则名称" stopProcessing="true">
<match url="正则表达式" />
<conditions>
<add input="{变量}" pattern="正则表达式" />
</conditions>
<action type="Redirect" url="目标地址" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
| 部分 | 作用 |
|---|---|
match url |
匹配请求的路径部分(不含开头的 /,不含域名和查询字符串) |
conditions |
额外条件:域名、HTTPS 状态、文件是否存在等 |
action |
Redirect(跳转,浏览器地址变化)、Rewrite(内部重写,地址不变)、AbortRequest、CustomResponse |
stopProcessing |
匹配后不再处理后续规则 |
redirectType |
Permanent(301)、Found(302) |
规则按顺序执行,所以跳转类规则通常放在最前面。
常用的条件变量
| 变量 | 含义 |
|---|---|
{HTTPS} |
on 或 off |
{HTTP_HOST} |
请求的域名 |
{REQUEST_URI} |
完整路径,含查询字符串 |
{QUERY_STRING} |
查询字符串 |
{REQUEST_FILENAME} |
请求对应的物理路径 |
{R:1} |
match 中第一个括号捕获的内容 |
{C:1} |
条件中第一个括号捕获的内容 |
规则一:HTTP 强制跳转 HTTPS
前提是网站已经配置好 HTTPS 绑定和证书,见 IIS 配置 SSL 证书。
<rule name="HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
{R:1} 不包含查询字符串,但 Redirect 动作默认会自动附加原来的查询字符串(appendQueryString 默认为 true)。
网站在 CDN 或负载均衡后面时,到达 IIS 的可能是 HTTP 请求,{HTTPS} 始终是 off,会导致无限跳转。这时改为判断代理传来的请求头,例如 {HTTP_X_FORWARDED_PROTO},见 重定向次数过多。
规则二:统一域名(不带 www 跳转到 www)
<rule name="Add www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
反过来(www 跳转到不带 www)把条件和目标对调即可。为什么要统一域名见 规范网址是什么。
规则三:旧网址 301 跳转到新网址
网站改版后,旧的网址要跳转到新地址,保留搜索引擎的收录和外部链接:
<rule name="Old product pages" stopProcessing="true">
<match url="^product\.php$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^id=(\d+)$" />
</conditions>
<action type="Redirect" url="/products/{C:1}/" appendQueryString="false" redirectType="Permanent" />
</rule>
把 product.php?id=123 跳转到 /products/123/。注意:
- 查询字符串不在
match url中,要在条件里用{QUERY_STRING}匹配 appendQueryString="false",否则目标地址后面还会带上?id=123
大量一对一的跳转:重写映射
几十上百条一一对应的旧网址,用重写映射(rewriteMaps)比写很多条规则清晰:
<rewrite>
<rewriteMaps>
<rewriteMap name="OldUrls">
<add key="/about-us.html" value="/about/" />
<add key="/contact.html" value="/contact/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Old URL map" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{OldUrls:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
跳转的 SEO 影响见 301 和 302 重定向的区别。
规则四:PHP 程序的伪静态(以 WordPress 为例)
请求的文件或目录不存在时,交给 index.php 处理:
<rule name="WordPress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
matchType="IsFile"+negate="true":文件不存在- 动作是
Rewrite,浏览器地址不变
其他 PHP 程序(ThinkPHP、Laravel 等)的规则类似,具体以程序的官方文档为准。Laravel 的入口文件在 public 目录,网站根目录应该指向 public。
规则五:单页应用(Vue / React)的回退
前端路由使用 history 模式时,直接访问 /user/profile 会 404,需要回退到 index.html:
<rule name="SPA fallback" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/api/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
排除 /api/ 路径,避免接口请求也被重写成 index.html。
规则的组合顺序
一个完整的 web.config 中,规则顺序通常是:
- HTTP → HTTPS
- 统一域名
- 旧网址跳转
- 伪静态 / 回退
前三条都带 stopProcessing="true"。建议把 HTTPS 和域名合并考虑:用户访问 http://example.com/a 时,最好一次跳转到 https://www.example.com/a,而不是先跳到 https://example.com/a 再跳到 www,多一次跳转就多一次延迟。
测试
curl.exe -I http://example.com/test?x=1
curl.exe -I https://example.com/product.php?id=123
Windows 10 / Server 2019 及以上自带 curl.exe。查看返回的状态码和 Location 头。PowerShell 中的 curl 可能是 Invoke-WebRequest 的别名,所以写 curl.exe。
测试 301 跳转时,浏览器会缓存 301,修改规则后浏览器可能仍然按旧的跳转执行。测试时使用 curl 或无痕窗口,规则调试阶段可以先用 302。
常见错误
修改 web.config 后网站 500 错误
- web.config 的 XML 格式错误(标签不匹配、引号缺失)
- 服务器没有安装 URL Rewrite 模块,但 web.config 中使用了
<rewrite>节点
开启详细错误信息(只在排查时临时开启),查看具体的错误行。
规则不生效
- 规则顺序问题:前面的规则已经
stopProcessing match url中写了开头的/。IIS 在网站级别匹配时,路径不包含开头的斜杠- 正则大小写:默认忽略大小写(
ignoreCase="true")
无限跳转
见上面关于 CDN 和 {HTTPS} 的说明,以及 重定向次数过多。
小结
- IIS 的跳转和伪静态需要安装 URL Rewrite 模块,规则写在 web.config 中
- match 只匹配路径(不含开头斜杠和查询字符串),查询字符串在条件中用
{QUERY_STRING}匹配 - Redirect 改变浏览器地址,Rewrite 内部重写;跳转规则放在前面并
stopProcessing - 大量一对一跳转用重写映射;伪静态用「文件和目录不存在」条件
- 用 curl 测试,注意浏览器缓存 301
常见问题
Apache 的 .htaccess 规则能直接用吗?
不能直接使用,但 IIS 管理器的「URL 重写」中有「导入规则」功能,可以导入 mod_rewrite 格式的规则并转换,转换后需要检查结果。
规则写在哪个 web.config 里?
通常写在网站根目录的 web.config 中。子目录也可以有自己的 web.config,规则会叠加,路径相对于该子目录。
能实现反向代理吗?
可以,URL Rewrite 配合 ARR(Application Request Routing)模块实现反向代理,见 IIS 反向代理配置。
还拿不准选哪款?
把业务类型、访问人群和预算告诉我们,工程师 7×24 在线,工单平均 10 分钟内响应。
