asp.net - IIS 重定向在没有尾部斜线的情况下无法工作

标签 asp.net iis web-config http-redirect

我有一个正在停用的 Web 应用程序,我正在尝试将对旧应用程序的所有请求重定向到特定页面(显示替换链接和一条消息,建议用户更新他们的书签)。

我已经使用这个问题的答案在 web.config 中设置了一个 HTTP 重定向:ASP.NET httpRedirect : redirect all pages except one除非用户输入网站根文件夹的 URL 时省略尾部斜杠,否则它会工作,在这种情况下,它会转到树上的下一个目录,例如:

原始站点根目录:[domain]/foo/bar/

[domain]/foo/bar/specificpage.aspx 重定向到 [domain]/foo/bar/Default.aspx (OK)

[domain]/foo/bar/ 重定向到 [domain]/foo/bar/Default.aspx(确定)

[domain]/foo/bar 重定向到 [domain]/foo/Default.aspx(不行)

这是相关的 web.config:

<system.webServer>
    <httpRedirect enabled="true" destination="~/Default.aspx" httpResponseStatus="Permanent">
      <add wildcard="/" destination="Default.aspx" />
    </httpRedirect>
  </system.webServer>
  <location path="Default.aspx">
    <system.webServer>
      <httpRedirect enabled="false" />
    </system.webServer>
  </location>

当用户访问 [domain]/foo/bar 时,我如何让它工作?

最佳答案

郑重声明,我通过将重定向 URL 设置为文件的绝对路径(包括网站根目录所在的虚拟目录)来修复此问题:

<system.webServer>
    <httpRedirect enabled="true" destination="/foo/bar/Default.aspx" httpResponseStatus="Permanent" exactDestination="true">
      <add wildcard="/" destination="Default.aspx" />
    </httpRedirect>
  </system.webServer>
  <location path="Default.aspx">
    <system.webServer>
      <httpRedirect enabled="false" />
    </system.webServer>
  </location>

关于asp.net - IIS 重定向在没有尾部斜线的情况下无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703502/

相关文章:

c# - 无法使用模板字段更新我的网格

c# - NHibernate 在修改现有实体时不触发更新语句

perl - 无法捕获 DBI 错误

c# - web.config 文件在 C# 中如何工作?

asp.net - 我找不到数据库

asp.net - 从 WCF 中的 ASP.NET RESTful Web 服务返回错误的最佳实践

c# - asp.net中如何输入3组数据

html - 即使 url 是新的,浏览器也不会下载更新的 css、js 等

asp.net - IIS 默认是否缓存静态内容?

c# - ASP.net编译项目错误