IIS-删除目录后的斜杠

时间:2018-08-24 19:03:01

标签: html redirect iis web-config

我想从“ /”到“”的所有请求中删除尾部斜杠,即使它是目录。例如:

我想将其重定向到“ http://bleh.local/sign-in

  • 预期的行为: /登录/ 重定向到 /登录
  • 实际行为: /登录重定向到 / sign-in /

Web.config:

<rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Redirect" url="{R:1}" />
</rule>

applicationHost.config:

<sectionGroup name="rewrite">
    <section name="globalRules" overrideModeDefault="Allow" allowDefinition="AppHostOnly" />
    <section name="rules" overrideModeDefault="Allow" />
    <section name="outboundRules" overrideModeDefault="Allow" />
    <section name="providers" overrideModeDefault="Allow" />
    <section name="rewriteMaps" overrideModeDefault="Allow" />
    <section name="allowedServerVariables" overrideModeDefault="Allow" />
</sectionGroup>

使用此设置,我得到了无限重定向,因为我从原始设置中删除了此指令:

<conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>

我认为我的问题来自于此:https://support.microsoft.com/en-ca/help/298408/iis-generates-courtesy-redirect-when-folder-without-trailing-slash-is

有什么想法吗?

注意:How can I configure IIS to serve directories without trailing slashes and always serve relative to root path?不能解决我的问题。反斜杠被一致地添加。

1 个答案:

答案 0 :(得分:0)

这是因为IIS中的<defaultDocument>

默认情况下,IIS中启用了

<defaultDocument>规则,如果未找到文件,该规则将强制页面以尾部斜杠重定向到url。 domain/sign-in/

<rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Redirect" url="{R:1}" />
</rule>

现在,此规则将url加上斜杠(并匹配目录),然后重定向到没有斜杠的url。 domain/sign-in会创建一个循环。

解决方案是添加规则<defaultDocument enabled="false"></defaultDocument>

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/

相关问题