0
votes

I have two domains, one is shortened and is used when sending text messages with a query string because the regular domain is too long. Currently, a web page on a different IP address redirects the short domain to the long one, either to a particular page if it has the correct query string, or to the home page if it does not. I'm moving to Azure where I will have only one IP address. So I figured IIS URL Rewrite would be able to handle this task. Long domain is a HTTPS only site, and there is a HTTPS rule for it. Short domain is not; the links are always HTTP only. I've setup URL Rewrite to do the Redirects before the HTTPS rule and stopProcessing="true" is set for both rules. But when I visit http://mytxt.net, I get a browser warning that the SSL cert is invalid.

The server is Windows Server 2016 IIS 10. I've searched Google and Stack Oveflow specifically but haven't found anything matching my issue. Below is the code.

        <rule name="Txt QS Redirect" stopProcessing="true">
          <match url="^(www\.)?mytxt\.net"/>
          <conditions>
            <add input="{QUERY_STRING}" pattern="^MyQS"/>
          </conditions>
          <action type="Redirect" url="https://www.myfullsite.net/respond.aspx" appendQueryString="true" redirectType="Temporary"/>
        </rule>
        <rule name="Txt No QS Redirect" stopProcessing="true">
          <match url="^(www\.)?mytxt\.net"/>
          <conditions trackAllCaptures="false">
            <add input="{QUERY_STRING}" pattern="^MyQS" negate="true"/>
          </conditions>
          <action type="Redirect" url="https://www.myfullsite.net/" redirectType="Permanent"/>
        </rule>
        <rule name="HTTPS Redirect">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$"/>
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
        </rule>

Shouldn't the redirect happen first, and then the protocol change to HTTPS? Or are the browsers checking for SSL first, and when IIS says it is enabled, doing the protocol change on the client?

1
Your certificate must work with both domains or you have two SNI bindings for each of them. In short, https must work for both sites, before you create the rewrite rule.Lex Li
@LexLi thank you but I solved the problem without altering the SSL certificate (see answer below).CB_Ron

1 Answers

0
votes

I solved my problem. It was the match url= regex that was the problem. Perhaps some IIS URL Rewrite guru can tell us why, because I still don't understand it. I used Failed Request Tracing to discover that it was not matching. This method is invaluable for troubleshooting URL Rewrite problems! I changed it to match url=".*" and moved the original regex to a condition. Here is the working code.

<rule name="Txt QS Redirect" stopProcessing="true">
  <match url=".*"/>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="^(www\.)?mytxt\.net"/>
    <add input="{QUERY_STRING}" pattern="^MyQS"/>
  </conditions>
  <action type="Redirect" url="https://www.myfullsite.net/respond.aspx" appendQueryString="true" redirectType="Temporary"/>
</rule>
<rule name="Txt No QS Redirect" stopProcessing="true">
  <match url=".*"/>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="^(www\.)?mytxt\.net"/>
    <add input="{QUERY_STRING}" pattern="^MyQS" negate="true"/>
  </conditions>
  <action type="Redirect" url="https://www.myfullsite.net/" redirectType="Permanent"/>
</rule>

URLs, with or without www., that start with the proper query string get redirected to the page that handles those requests; without the proper query string they are redirected to the site's home page.