简体   繁体   中英

IIS 8.5 URL Rewrite for https and www for Subfolder MVC app

I need to rewrite rules for MVC app that created in IIS sub folder (www.example.com/test/App is application root)

Rewrite rules will redirect non-www request to www and http request to https

Cases:

A) Will be redirect to: 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) Will be redirect to: https://www.example.com/test/App

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

I tried routings below, but these are not redirects cases under A section.

Is there any rule or rules for achieve both A and B cases?

<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" />

You can achieve that with two rules:

<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>

You just need to replace example.com with your actual domain name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM