【问题标题】:IIS - Remove trailing slash when directoryIIS - 目录时删除尾部斜杠
【发布时间】:2018-08-24 19:03:00
【问题描述】:

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

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

  • 预期行为:/sign-in/ 重定向到 /sign-in
  • 实际行为:/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 天开始进入同样的问题。不幸的是,互联网上没有任何帮助。

标签: html redirect iis web-config


【解决方案1】:

这是因为 IIS 中的 &lt;defaultDocument&gt;

&lt;defaultDocument&gt; 规则默认在 IIS 中启用,如果页面未找到文件,则强制将页面重定向到带有斜杠的 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 与尾部斜杠匹配(并匹配目录),然后重定向到不带斜杠的那个。 domain/sign-in 创建一个循环。

解决方法是添加规则&lt;defaultDocument enabled="false"&gt;&lt;/defaultDocument&gt;

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

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 2013-04-30
    • 2014-05-26
    • 2015-06-20
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多