【问题标题】:How to apply URL Rewrite rules correctly for IIS applications如何为 IIS 应用程序正确应用 URL 重写规则
【发布时间】:2017-01-28 02:39:09
【问题描述】:

我们有一个 IIS 8.5 设置,其中单个网站绑定到 domain.com 并包含许多 IIS 应用程序,这些应用程序以 domain.com/app1、domain.com/app2 等方式访问。

这些应用程序中的每一个都指向相同的物理路径,因此它们都共享一个 web.config。这是特定的 CMS 配置。

我已将通常的 URL 重写规则(重定向到 HTTPS、强制小写、添加斜杠等)应用于每个应用程序共享的 web.config,但我意识到这些规则仅适用于应用程序名称之后的 URL。我拥有的规则只是使用 URL 重写 GUI 添加的标准规则:

<rewrite>
  <rules>
    <rule name="Enforce lowercase" stopProcessing="true">
      <match url="[A-Z]" ignoreCase="false" />
      <action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />
    </rule>
    <rule name="Add trailing slash" stopProcessing="true">
        <match url="(.*[^/])$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
    </rule>
    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

因此,例如,http://domain.com/APP1/PATH 重定向到 https://domain.com/APP1/path/。此外,https://domain.com/app1不会重定向到https://domain.com/app1/

HTTPS 规则很好,但谁能告诉我如何配置其他 2 条规则,以便它们与整个 URL 一起使用,请记住,特定的应用程序名称(app1、app2 等)需要通用处理.

更新

我发现我可以使用 IIS 中的全局规则(在服务器级别)强制使用小写 URL,这足以满足我的需要。但似乎无法复制用于添加/删除尾部斜杠的网站级规则。

【问题讨论】:

    标签: iis seo url-rewrite-module


    【解决方案1】:

    您只需要更改操作 URL。

       <rule name="Add trailing slash" stopProcessing="true">
            <match url="(.*[^/])$" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}/" redirectType="Permanent" />
        </rule>
    

    【讨论】:

    • 抱歉,这无助于在根 URL 中添加斜杠(例如 domain.com/app1),因为在这种情况下,规则甚至不会运行。
    • 这是我在我的网站上使用的规则,不知道为什么它适用于我而不是你。也许你有其他规则干扰它?
    • 我想如果你再次测试你会发现该规则只适用于相对路径,所以它不会将 domain.com 重定向到 domain.com/ (或者在我的情况下是 domain.com/ app 到 domain.com/app/ 因为 app 是一个 IIS 应用程序)
    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2014-07-03
    相关资源
    最近更新 更多