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

问题描述:

我们有一个IIS 8.5安装程序,其中一个网站绑定到domain.com,并包含许多IIS应用程序,作为domain.com/app1,domain.com/app2等访问如何正确地为IIS应用程序应用URL重写规则

这些应用程序中的每一个指向相同的物理路径,因此它们都共享一个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等)需要处理一般。

UPDATE

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

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

对不起,这不会帮助将尾部斜杠添加到根URL(例如https://domain.com/app1),因为在这种情况下,规则将不会运行。 – getsetcode

+0

这是我在我的网站上使用的规则,不知道为什么它对我有用,而不是你。也许你有另一个规则干扰它? – Henry

+0

我想如果你再次测试,你会发现该规则只适用于相对路径,所以它不会将domain.com重定向到domain.com/(或者在我的情况下domain.com/app到domain.com/app /因为应用程序是一个IIS应用程序) – getsetcode