Search code examples
asp.netasp.net-mvchttp-redirectiisiis-7.5

Conditoinal HTTP redirect IIS 7


Currently I am redirecting all my http requests to HTTPS but now I want to add the condition that if URL contains 'admin' then redirect HTTP requests to HTTPS, how can i change the Rule to achieve this (e.g. http://www.example.com/admin/index.html) ?

 <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
    </rule>

Solution

  • Change the url pattern to:

    <match url=".*admin/.*" />
    

    This seems to match your sample data OK in the IIS pattern match tool.

    To match additional portions, e.g. profile you can alter the regular expression:

    <match url=".*(admin|profile)/.*" />
    

    If it is always going to start with admin or profile then you can tighten the regular expression to ^(admin|profile)/.* (^ means start of pattern, see Defining a Pattern on this screen).