0
votes

I have a Windows 2008 server running IIS7.5 using URL Rewrite.

I have a URL and want all permutations of this URL to redirect to a secure https version with www. So for example I want the following:

To redirect to:

https://www.mydomain.ext

I have set up 3 rewrite rules but unfortunately I cannot get https://mydomain.ext to redirect.

Here are the rewrites I use with the middle one not working. However I would prefer a single rule to cover all instances.

    <!-- Redirect http non www to https www -->
    <rule name="Redirect http://mydomain.ext to www" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="mydomain.ext" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>

    <!-- Redirect https non www to http www -->
    <rule name="Redirect https://mydomain.ext to www" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="https://mydomain.ext" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>

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

1 Answers

0
votes

The problem with the rule above is that the protocol (https) is never part of the host (HTTP_HOST), so your rule will never match. You only need two rules, but make sure they are the first ones and stop processing rules (since redirects should usually stop them anyway). so this should work. Note that the other critical thing is to use "full string match using ^ and $", you could also do a rule that does the inverse to redirect all that is not that specific domain to it (see at the bottom):

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

<!-- Redirect http non www to https www -->
<rule name="Redirect mydomain.ext to www" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^mydomain.ext$" />
  </conditions>
  <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

If you are 100% sure that you only want to use a domain, and no other way, you can stop any permutations that ever make it and redirect to the canonical one using this (instead of the second rule above, in this case using negate)

    <rule name="Mydomain" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^www.mydomain.ext$" negate="true" />
        </conditions>
        <action type="Redirect" url="https://www.mydomain.ext/{R:1}" />
    </rule>