0
votes

I have a URL and I have applied an SSL certificate to the www variant https://www.example.com. I would like all variations of this domain to point to https://www.example.com,

So for example the following domains should redirect to https://www.example.com:

The website is hosted on a windows 2008 server running IIS7.5 and I have created some rules in the web.config file using URL Rewrite. However the following domains do not redirect:

Here are the rules I currently have:

    <!-- Redirect http non www to https www -->
    <rule name="Redirect http://example.com to www" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="example.com" />
      </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>

I would appreciate some assistance in getting this to work using a URL rewrite. Ideally I would prefer to replace the above with a single rule.

2

2 Answers

2
votes

Let's follow the logic below, this is using "Regex".

Match any URL being passed in, then match any of the follow conditions.

  1. If the host starts matches example.com then redirect
  2. If the port 80 is being used than redirect.

This will cover redirecting http to https and anything example.com to www.example.com

<rule name="Do It All" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="^example\.com$" />
    <add input="{SERVER_PORT}" pattern="^80$" />
  </conditions>
  <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
0
votes

You are better off using two rules, the first to redirect all non-https to https without stopProcessing=true, and the second to redirect all non-www to www, like so.

<rule name="HTTPS Redirect">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTPS}" pattern="^OFF$" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to www">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="^example\.com$" />
  </conditions>
  <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

Also, you don't need appendQueryString=true; {R:1} appends the query string. stopProcessing=true is also not necessary, especially if you don't have any other rules following. Most of the time though, if you do have rules following, you want them to execute after the URL has been redirected to https and reformatted to www.