2

I have a site that we just setup with SSL and have a redirect rule set up in web.config. We also use the www. as the preferred domain. The problem I have is I want to skip the middle step in the redirect.

What happens now is if you enter http://example.com, it redirects first to https://example.com, then again to https://www.example.com.

How can I setup the config to redirect straight from http://example.com to https://www.example.com?

Here is the rule I am using now in my web.config.

<rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions> 
        <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule> 
  • Aside: If you have any plans to implement HSTS then you will actually need that middle step. ie. You should redirect to HTTPS on the same host before redirecting to canonicalise the www/non-www. – MrWhite Sep 5 '18 at 14:54
1

I have found an answer from another question that I tried and it seems to have worked. If there is a better solution, please provide your solution as well.

<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
  <add input="{HTTP_HOST}" pattern="^[^www]" />
  <add input="{HTTPS}" pattern="off" />
  </conditions>
  <action type="Redirect" url="https://www.example.com/{R:1}" 
appendQueryString="true" redirectType="Permanent" />
</rule>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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