简体   繁体   中英

web.config redirect multiple domains to one AND redirect HTTP to HTTPs

I am using web.config to redirect all HTTP traffic to a HTTPs website

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

This works just fine.

My client wants me to now redirect a whole load of other non HTTPs .com domains to this HTTPs .co.uk domain.

I found a redirect script:

<rule name="Redirect to www.MYDOMAIN.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
    <match url=".*" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTP_HOST}" pattern="^(www.)?MYOTHERDOMAIN1.(com|org)$" />
            <add input="{HTTP_HOST}" pattern="^(www.)?MYOTHERDOMAIN2.(com|net|org)$" />
        </conditions>
    <action type="Redirect" url="http://www.MYDOMAIN.co.uk/{R:0}" />
</rule>

For the life of me I cannot seem to combine these script into one script that will pick any of his domains and cleanly point them in the direction of the HTTPs .co.uk domain.

Any ideas would be greatly appreciated as regular expressions are not my strong point.

Kind Regards

You can simply add two rules in the order they must process (with only the last rule set to stopProcessing).

  <rules>
    <clear />
    <rule name="domain redirect" stopProcessing="false">
      <match url="(.*)" />
      <conditions>
    <add input="{HTTP_HOST}" pattern="^(www.)?EXAMPLE2.(com|net)$" />
      </conditions>
      <action type="Redirect" url="http://www.EXAMPLE.net{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
    </rule>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
    </rule>
  </rules>

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