24

I'm working on a website that uses IIS 7's URL rewriting feature to do a permanent redirect from example.com to www.example.com, as well as rewrites from similar domain names to the "main" one, such as from www.examples.com to www.example.com.

This rewrite rule - shown below - has worked well for some time now. However, we recently added HTTPS support and noticed that if users visit one of the URLs to be rewritten to www.example.com then HTTPS is dropped. For instance, if a user visits https://example.com they get redirected to http://www.example.com, whereas we would like them to be sent to https://www.example.com.

Here is the rewrite rule of interest (in Web.config):

<rule name="Canonical Host Name" stopProcessing="true">
    <match url="(.*)" />

    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
        <add input="{HTTP_HOST}" pattern="^(www\.)?example\.net$" />
        <add input="{HTTP_HOST}" pattern="^(www\.)?example\.info$" />
        <add input="{HTTP_HOST}" pattern="^(www\.)?examples\.com$" />
    </conditions>

    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

As you can see, the action element's url attribute points directly to http://, so I get why https://example.com is redirected to http://www.example.com. My question is, how do I fix this? I tried (naively) to just drop the http:// part from the url attribute, but that didn't work.

1
  • It appears that simply leaving HTTP:// out of the redirect URL will cause IIS 7.5 to use the inbound request's protocol.
    – HABO
    Sep 12, 2012 at 15:13

4 Answers 4

40

Here's Scott's answer with Hasan's improvements. This should cover mixed SSL/non-SSL sites. The rule basically says "if the url does not have www.example.com", do a permanent redirect to it. Essentially... you are redirecting people who visit you without www or directly to your IP address.

<rewrite>
<rules>
    <rule name="Canonical Host Name" stopProcessing="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
            <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
        </conditions>
        <action type="Redirect" url="{MapSSL:{HTTPS}}www.example.com/{R:1}" redirectType="Permanent" />
    </rule>
</rules>
<rewriteMaps>
    <rewriteMap name="MapSSL" defaultValue="http://">
        <add key="ON" value="https://" />
        <add key="OFF" value="http://" />
    </rewriteMap>
</rewriteMaps>
</rewrite>
3
  • @Douglas The defaultValue attribute specifies the default input parameter, not return value. In this case "OFF" is the default value if none is passed.
    – Matt Borja
    Mar 16, 2016 at 20:49
  • @MattBorja: Are you sure? <rewriteMap name="StaticRewrites" defaultValue=""> "The defaultValue attribute specifies what value to use if an incoming URL has not been defined in the map. In this case an empty string will be returned."
    – Douglas
    Mar 16, 2016 at 21:01
  • Just did a test to confirm and it looks like it is the default return value and not the input parameter as I originally thought. So in the answer, it would write out OFFwww.example.com if for whatever reason the {HTTPS} serverVariable returned anything but ON or OFF (which you would probably never see happen...meh)
    – Matt Borja
    Mar 16, 2016 at 21:09
11

Figured out the answer with some help from my colleagues.

I needed to use multiple rules with a condition on {HTTPS}. Note the {HTTPS} condition in the rules below.

<rule name="Canonical Host Name (HTTP)" stopProcessing="true">
    <match url="(.*)" />

    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="OFF" />
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
    </conditions>

    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

<rule name="Canonical Host Name (HTTPS)" stopProcessing="true">
    <match url="(.*)" />

    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="ON" />
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
    </conditions>

    <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

I then repeated the rule pair above for the alternate domain names.

3
  • 2
    I believe you meant for those to say "MatchAll" instead of "MatchAny". Otherwise you get stuck in a loop of death and your page stops loading.
    – Ralph N
    Apr 19, 2012 at 11:45
  • The first rule should mean "if it's HTTPS pattern off AND the HTTP_HOST url looks like this pattern". The second rule should mean "if it's HTTPS pattern off AND the HTTP_HOST url looks like this pattern".
    – Ralph N
    Apr 19, 2012 at 11:54
  • @Scott Mitchel will you please write some explaination too..for the symbols used in pattern? Feb 18, 2016 at 8:31
8

If you just want to redirect based on the currently used protocol (as per your last sample) then there's a much simpler solution that will halve the amount of rules you will need. The following is what I've learned from a collegue of mine.

As you've seen, the {HTTPS} argument will contain the value ON or OFF. You can map this value to https:// or http:// by feeding this value into a rewritemap.

Here's how this would work:

1- Create a rewritemap section for mapping the {HTTPS} value:

    <rewriteMap name="MapProtocol" defaultValue="OFF">
      <add key="ON" value="https://" />
      <add key="OFF" value="http://" />
    </rewriteMap>

It's up to you to decide if you want to only include the protocol, or the semicolon and forward slashes as well. It doesn't matter for the solution, but keep it in mind wherever you refer to it.

2- Refer to this map wherever you need. In this sample it's used in outbound-rules, but it'll also work in your scenario:

    <rule name="Outbound-Rule Name" stopProcessing="true" preCondition="ResponseIsHtml">
      <match filterByTags="A, Link, Script" pattern="YOUR PATTERN" />
      <action type="Rewrite" value="{MapProtocol:{HTTPS}}{HTTP_HOST}/REST OF RELATIVE LINK HERE" />
    </rule>

That's it, the URL Rewrite module should now automagically use the correct protocol for your links depending on if you're using https, or, of course, http.

Hope this helps!

1
  • This definitely improves Scotts answer. I'll make a third reply combining both of your answers to show what the config should look like.
    – Ralph N
    Apr 19, 2012 at 12:15
1

Here's a cross-domain solution which works not only on example.com but also on any domain

<rewrite>
    <rules>
        <rule name="Canonical Host Name" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
                <add input="{HTTP_HOST}" pattern="^www\.([.a-zA-Z0-9]+)$" negate="true" />
            </conditions>
            <action type="Redirect" url="{MapProtocol:{HTTPS}}www.{HTTP_HOST}/{R:0}" redirectType="Permanent" />
        </rule>
    </rules>
    <rewriteMaps>
        <rewriteMap name="MapProtocol" defaultValue="OFF">
            <add key="ON" value="https://" />
            <add key="OFF" value="http://" />
        </rewriteMap>
    </rewriteMaps>
</rewrite>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.