You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Web App忽略web.config URL重写规则问题排查求助

问题描述

部署了一个以.NET 8.0为目标、运行时为可移植模式的框架依赖型Azure Web App,已将web.config放置在wwwroot目录中,希望通过URL重写将默认域名sitename.azurewebsites.net永久重定向至自定义域名https://sitename.com,禁止访问默认站点,但web.config中的重写规则始终被忽略。已确认web.config已发布至站点根目录,原配置内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^sitename\.azurewebsites\.net$" />
                        </conditions>
                        <action type="Redirect" url="https://sitename.com.au/{R:0}" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments="\.\sitename.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>
问题排查与修复

导致重写规则失效的核心问题及修复方案如下:

  • 应用启动参数错误:<aspNetCore>节点的arguments属性使用了无效路径\.\sitename.dll,多余的前置反斜杠会导致dotnet命令无法找到应用程序集,应用启动失败,间接使IIS重写规则无法正常执行。修正为正确的相对路径.\sitename.dll。
  • 重定向目标域名不符:配置中重定向目标为https://sitename.com.au/{R:0},与需求的https://sitename.com不一致,需修正为目标域名。
  • 规则名称拼写错误:原规则名称中的rquests应为requests,虽不影响功能,但建议修正以保证配置规范性。
修正后的web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect requests to default Azure websites domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^sitename\.azurewebsites\.net$" />
                        </conditions>
                        <action type="Redirect" url="https://sitename.com/{R:0}" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\sitename.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>
验证步骤
  1. 登录Azure门户,进入Web App的「配置」>「常规设置」,确认运行时栈为.NET 8 (STS),运行时为「可移植」。
  2. 通过Kudu控制台检查wwwroot目录下的web.config是否为修正后的版本。
  3. 重启Web App后,访问sitename.azurewebsites.net,验证是否自动重定向至https://sitename.com。

内容的提问来源于stack exchange,提问作者user3500162

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 04:17:05