IIS URL重写:添加除.html和.aspx之外的尾部斜杠

Seb*_*son 19 asp.net iis url-rewriting url-rewrite-module

通过IIS URL重写模块向所有URL添加尾部斜杠广泛传播,但如何为以.html和.aspx结尾的URL添加例外

今天我有这个:

<rule name="Add trailing slash" stopProcessing="true">
  <match url="(.*[^/])$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <!-- Doesn't seem to be working -->
    <!--<add input="{REQUEST_URI}" pattern="(.*?).html$" negate="true" />-->
    <!--<add input="{REQUEST_URI}" pattern="(.*?).aspx$" negate="true" />-->
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
Run Code Online (Sandbox Code Playgroud)

Seb*_*son 25

如果你想做正确的事情,你必须自己做,显然......

这是我的问题的解决方案:

<rule name="Add trailing slash" stopProcessing="true">
  <match url="(.*[^/])$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
Run Code Online (Sandbox Code Playgroud)

更新:我更详细地在博客上发表了这篇文章.

  • 你为什么要否定 matchType="IsDirectory"?这不正是应该添加尾部斜杠的情况吗? (3认同)

Gis*_*mby 14

改变其他答案,我使用了这个,所以我不必指定文件扩展名列表:

<!-- Ensure trailing slash -->
<rule name="Add trailing slash" stopProcessing="true">
  <match url="(.*[^/])$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule> 
Run Code Online (Sandbox Code Playgroud)