0
votes

My IIS website is currently accessed as http://mywebsite.com and I have 3 objectives related to url rewriting:

  1. Redirect to the website's new name, mynewwebsite.com
  2. Remove www prefix if it exists (eg www.mywebsite.com -> mywebsite.com or www.mynewwebsite.com --> mynewwebsite.com
  3. Redirect to SSL

So in summary I want to redirect all inbound requests to https://mynewwebsite.com/anything*.

The input cases that must be accounted for are

My rewrite rules look as follows:

<rewrite>
  <rules>
    <rule name="Do stuff" enabled="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" />
        <add input="{HTTP_HOST}" pattern="mywebsite.com" />
        <add input="{HTTPS}" pattern="^www.mynewwebsite\.com$" />
      </conditions>
      <action type="Redirect" url="https://mynewwebsite.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

With this rule set the following input case is failing (the www isn't stripped): https://www.mynewwebsite.com/anything*

I've banged my head against this sufficiently that I question my fundamental understanding of how rule definitions work.

  • anything could be present or not exist at all
1

1 Answers

0
votes

I arrived at the following rules which work, but I am afraid it is by coincidence rather than me really understanding what's happening. Comments welcome.

<rewrite>
  <rules>
    <rule name =" Force HTTPS" enabled="true">
      <match url =" (.*)" ignoreCase= "false " />
      <conditions logicalGrouping =" MatchAny" >
        <add input =" {HTTP_HOST}" negate="true" pattern =" ^mynewwebsite\.com$" />
        <add input =" {HTTPS}" pattern= "^www.mynewwebsite\.com$ " />
        </conditions>
      <action type =" Redirect" url= "https://mynewwebsite.fm/{R:1} " appendQueryString="true" redirectType =" Permanent" />
    </rule>
  </rules>
</rewrite>