-->

IIS URL重写:添加斜线除了.html和的.aspx(IIS URL Rewrite: Add

2019-08-01 03:31发布

添加斜线通过IIS URL重写模块的所有URL被广为流传,但我怎么添加对网址的例外与.html和结束的.aspx?

今天,我有这样的:

<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>

Answer 1:

如果你想要的东西做对,你得自己做,很明显...

这是解决我的问题:

<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>

更新 : 我更详细的博客上讲述这个 。



Answer 2:

不同其他的答案,我用这个,所以我不会有指定的文件扩展名的列表:

<!-- 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> 


Answer 3:

我们像下面这样添加多个扩展名:

<add input="{URL}" negate="true" pattern="((.+).(jpg|ico|gif|js|png|htm|css|html))" ignoreCase="true" />


Answer 4:

要阻止所有文件从已经增加了斜线,我改变了比赛规则如下:

<match url="^([^.]*[^/])$" />

适用该规则只对包括任何数量的不以斜线结束的非点的字符的路径。 从而使包括点的任何路径(例如xxx.html,xxx.aspx等)将被排除在外,而无需任何额外的否定规则。

寻找一个点的匹配规则的存在让我完全删除使用匹配类型ISFILE和IsDirectory的条件规则。 这些匹配类型只允许在分布式规则(web.config中),而不是在全球规则(的applicationHost.config),所以我被迫复制此规则对于每个站点,而不是把它应用到使用全局规则中的所有网站。 通过修改正则表达式的匹配规则要排除的文件和去除ISFILE和IsDirectory条件,我能创造一个全球性的规则,而不是有多个分布式的规则。



Answer 5:

这几乎是为我工作。 我不得不将其更改为

<add input="{URL}" pattern="(.*?)\.html$" negate="true" />
<add input="{URL}" pattern="(.*?)\.aspx$" negate="true" />

否则,感谢这个!



文章来源: IIS URL Rewrite: Add trailing slash except for .html and .aspx