如何修复web.config中失效的重写规则?HTTP重定向正常但报500错误
问题描述
我正尝试通过web.config文件配置重定向,其中httpRedirect规则可正常运行,但rewrite规则失效并返回500错误。进入IIS的HTTP Redirect界面时显示异常(见下图),移除rewrite节点后该界面可正常打开。请问我哪里操作有误?

当前web.config配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" /> </system.web> <system.webServer> <rewrite> <rules> <rule name="rewrite_client_contract" stopProcessing="true"> <match url="^client/contractreview/index/(.*)" /> <action type="Redirect" url="https://adumbsite.com/contractreview/index/{R:1}" appendQueryString="false" redirectType="Permanent" /> </rule> </rules> </rewrite> <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent"> <add wildcard="/public/signup" destination="https://adumbsite.com/sign-up/" /> </httpRedirect> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> <system.web> <customErrors mode="Off"/> </system.web> </configuration>
问题根源
- XML配置结构非法:你的web.config里存在两个独立的
<system.web>节点,XML规范要求同一父节点下不能重复定义同名节点,这会导致IIS解析配置时失败,直接触发500错误,同时破坏HTTP Redirect界面的正常加载逻辑。 - 额外需确认:IIS服务器是否已安装URL Rewrite模块,如果未安装,
<rewrite>节点会被识别为无效配置,同样会引发500错误。
解决方案
- 合并重复的
<system.web>节点:将两个<system.web>的内容合并为一个,确保XML结构合法。 - 检查URL Rewrite模块:打开IIS管理器的“模块”列表,确认存在
RewriteModule;若没有,需安装微软官方的URL Rewrite模块。
修正后的web.config配置:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" /> <customErrors mode="Off"/> </system.web> <system.webServer> <rewrite> <rules> <rule name="rewrite_client_contract" stopProcessing="true"> <match url="^client/contractreview/index/(.*)" /> <action type="Redirect" url="https://adumbsite.com/contractreview/index/{R:1}" appendQueryString="false" redirectType="Permanent" /> </rule> </rules> </rewrite> <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent"> <add wildcard="/public/signup" destination="https://adumbsite.com/sign-up/" /> </httpRedirect> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> </configuration>
内容的提问来源于stack exchange,提问作者geoff swartz
相关产品推荐
相关产品推荐

