IIS 8.5 URL重写为https和www为子文件夹MVC应用程序

时间:2017-01-12 11:27:24

标签: asp.net-mvc iis url-routing iis-8.5

我需要重写在IIS子文件夹中创建的MVC应用程序的规则(www.example.com/test/App是应用程序根目录)

重写规则会将非www请求重定向到www和http请求重定向到https

例:

A)将重定向到:https://www.example.com/test/App/Controller

1) http://www.example.com/test/App/Controller
2) example.com/test/App/Controller
3) www.example.com/test/App/Controller

B)将重定向到:https://www.example.com/test/App

1) http://www.example.com/test/App
2) example.com/App
3) www.example.com/test/App

我尝试了下面的路线,但这些不是A部分下的重定向案例。

是否有任何规则或规则来实现A和B案件?

<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
    <add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
<rule name="Redirect to WWW https" stopProcessing="true">
<match url=".*" />
<conditions>
    <add input="{HTTPS}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />

1 个答案:

答案 0 :(得分:0)

您可以通过两条规则实现这一目标:

<rule name="Redirect example.com to www" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^example.com$" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:0}" />
</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}" />
</rule>

您只需将example.com替换为您的实际域名

即可