iis - 如何为 IIS 应用程序正确应用 URL 重写规则

标签 iis seo url-rewrite-module

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

这些应用程序中的每一个都指向相同的物理路径,因此它们都共享一个 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,这足以满足我的需要。但似乎无法复制用于添加/删除尾部斜线的网站级规则。

最佳答案

您只需更改操作 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>

关于iis - 如何为 IIS 应用程序正确应用 URL 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39589557/

相关文章:

redirect - 如何设置域名以重定向到另一个 URL,然后检查来自该域的引荐来源网址?

php - 在 WordPress 中为自定义帖子类型存档添加元描述

c# - 在 ASP.NET 中插入 html 标题和元描述服务器端?

ssl - IIS 8 (Server 2012) 未指定站点名称时站点绑定(bind)不起作用

c# - 系统.ComponentModel.Win32Exception : Access is denied Error

search - 如何使我们的网站在谷歌搜索结果中名列前十?

IIS URL 重写模块 : Redirect Based On QueryString

asp.net - IIS 重写不起作用(但重定向起作用)

redirect - IIS 重写规则以将特定域 url 重定向到同一域上的不同 url

c# - SignalR 自主机连接重置