核心步骤:使用 IIS URL Rewrite 模块
1. 安装 URL Rewrite 模块
-
下载地址:IIS URL Rewrite 扩展
-
安装步骤:
-
下载 rewrite_amd64.msi
(x64 系统适用)。
-
双击安装,按向导完成。
-
重启 IIS 服务:打开命令提示符(管理员),运行 iisreset
。
2. 配置伪静态规则
方法一:通过 IIS 管理器(图形界面)
-
打开 IIS 管理器 > 选择您的网站。
-
双击 URL 重写 图标。
-
点击右侧 添加规则 > 选择 空白规则。
-
配置规则示例(将 /news/123
重写到 news.aspx?id=123
):
方法二:直接编辑 web.config
(推荐)
-
在网站根目录的 web.config
中添加 <rewrite>
节点:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to news.aspx" stopProcessing="true">
<match url="^news/([0-9]+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="news.aspx?id={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
3. 常见伪静态规则示例
4. 验证伪静态是否生效
-
访问测试 URL(如 http://yoursite.com/news/123
)。
-
检查页面内容是否与 news.aspx?id=123
一致。
-
在浏览器开发者工具中查看 Network 请求,确认状态码为 200
(而非 404
或 302
)。
5. 关键注意事项
-
权限问题:
-
处理 ASP.NET 路由冲突:
-
文件存在性检查:
-
规则顺序:
故障排查
通过以上步骤,您的 ASP.NET 网站即可在 Windows Server 2016 上实现安全可靠的伪静态功能。