【问题标题】:IIS URL Rewriting rule - exclude ALL files and a specific pathIIS URL 重写规则 - 排除所有文件和特定路径
【发布时间】:2018-08-29 11:03:06
【问题描述】:

我想创建一个 URL 重写规则,在没有的 URL 上添加 /,例如:

www.domain.com/news/latest 将被重定向到 www.domain.com/news/latest/

下面的规则正是这样做的,但我遇到的问题有两个:

  1. 此重写规则正在应用于图像文件等内容。 因此,例如 domain.com/globalassets/icons/image.svg 更改为 domain.com/globalassets/icons/image.svg/ 导致 404 它不会发生在 CSS 文件中,这很奇怪,可能是因为我在 MVC 中使用 RegisterBundles 方法添加它们?

  2. 这是一个使用 CMS(episerver)的基于 ASP.NET MVC 的网站,所以我想忽略管理区域中的任何重定向,所以我添加了第二条规则,但它再次对 CSS 和图像进行破坏管理区域。

这是我到目前为止所得到的,谁能帮我让这条规则正常工作?

<rewrite>
    <rules>

      <rule name="Exclude Slash Episerver " stopProcessing="true">
        <match url="^episerver/" />
        <action type="None" />
      </rule>

      <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" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
      </rule>

    </rules>
</rewrite>

【问题讨论】:

    标签: asp.net-mvc url-rewriting iis-7.5 url-rewrite-module


    【解决方案1】:

    IIS 的常见 SEO 重写规则,例如这个是documented here

    特别是,使用 尾部斜杠 规则,您会丢失 logicalGrouping="MatchAll" attribute

    条件在重写规则的&lt;conditions&gt; 集合中定义。该集合有一个名为logicalGrouping 的属性,它控制如何评估条件。如果规则具有条件,则仅当规则模式匹配并且:

    • 只要使用了 logicalGrouping="MatchAll",所有条件均被评估为真。
    • 如果使用了 logicalGrouping="MatchAny",则至少有一个条件被评估为真。

    如果没有此设置,它会在您的文件名中添加一个斜杠,因为当您的 任何 否定规则匹配而不是 所有 时,它会匹配。 p>


    上述链接中的整个尾随斜杠规则是:

    <rule name="Trailing Slash" stopProcessing="true"> 
        <match url="(.*[^/])$" /> 
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
            <add input="{URL}" pattern="WebResource.axd" negate="true" /> 
        </conditions> 
        <action type="Redirect" url="{R:1}/" /> 
    </rule>
    

    【讨论】:

      猜你喜欢
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 2020-02-17
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      相关资源
      最近更新 更多