13

My website has two bindings: 1000 and 1443 (port 80/443 are in use by another website on the same IIS instance). Port 1000 is HTTP, port 1443 is HTTPS. What I want to do is redirect any incoming request using "htt p://server:1000" to "https://server:1443". I'm playing around with IIS 7 rewrite module 2.0 but I'm banging my head against the wall. Any insight is appreciated!

BTW the rewrite configuration below works great with a site that has an HTTP binding on port 80 and HTTPS binding on port 443, but it doesn't work with my ports.

P.S. My URLs intentionally have spaces because the 'spam prevention mechanism' kicked in. For some reason google login doesn't work anymore so I had to create an OpenID account (No Script could be the culprit). I'm not sure how to get XML to display nicely so I added spaces after the opening brackets.

< ?xml version="1.0" encoding="utf-8"?>
< configuration>
  < system.webServer>
    < rewrite>
      < rules>
        < rule name="HTTP to HTTPS redirect" stopProcessing="true">
          < match url="(.*)" />
          < conditions trackAllCaptures="true">
                        < add input="{HTTPS}" pattern="off" />
          < /conditions>
          < action type="Redirect" redirectType="Found" url="htt ps: // {HTTP_HOST}/{R:1}" />
        < /rule>
      < /rules>
    < /rewrite>
  < /system.webServer>
< /configuration>
Andy Arismendi
  • 1,188
  • 5
  • 16
  • 27

3 Answers3

12

Even though the question was answered awhile ago, this is the rule that I used.

<rule name="Redirect to HTTP on different port" enabled="true" stopProcessing="true">
      <match url="(.*)"/>
      <conditions>
        <add input="{HTTPS}" pattern="ON"/>
      </conditions>
      <!-- Cannot Use {HTTP_HOST} as it contains both the {SERVER_NAME}{SERVER_PORT} -->
      <action type="Redirect" url="http://{SERVER_NAME}:1000{HTTP_URL}" redirectType="Found" appendQueryString="false"/>
    </rule>
Julien Jacobs
  • 221
  • 2
  • 4
9
            <rule name="HTTP to HTTPS on different SSL Port" enabled="true" stopProcessing="true">
                <match url="(.*)" ignoreCase="true" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                    <add input="{HTTPS}" pattern="off" />
                    <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
                </conditions>
                <action type="Redirect" url="https://{C:1}:1443/{R:0}" appendQueryString="false" />
            </rule>

This solved my problem.

Andy Arismendi
  • 1,188
  • 5
  • 16
  • 27
  • 2
    it should probably be: ([^/:]+)(:[^/]*)? since port might not always be there. – Leblanc Meneses Sep 18 '12 at 15:15
  • 2
    For any future googlers: it looks like this answer comes from [this thread](http://forums.iis.net/t/1174819.aspx?Need+to+redirect+HTTP+to+HTTPS+on+non+standard+ports) on the IIS forum. A complete description of the regex is given there. – Lasse Christiansen Aug 21 '14 at 21:24
1

Try changing this:

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}"/>

to this:

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}:1443/{R:1}"/>
Dscoduc
  • 1,095
  • 2
  • 8
  • 15
  • 1
    That doesn't seem to work. I wish the URL rewrite module had an easy way to see what it is changing the URL to. – Andy Arismendi Jan 19 '11 at 04:08
  • The matching and condition part work. When I hard code the value of the redirect URL it works, but I need the redirect URL to be dynamic. If a user types in http:// 10.0.0.100:1000 (server IP) it needs to redirect to https:// 10.0.0.100:1443. Same if they type in http:// server.domain.com:1000 it needs to redirect to https:// server.domain.com:1443. – Andy Arismendi Jan 19 '11 at 04:34
  • 1
    So it looks like part of the problem is that {HTTP_HOST} includes the port e.g. if I type http:// server:1000 then {HTTP_HOST} contains 'server:1000'. This doesn't seem right especially since there is a separate {SERVER_PORT} variable. – Andy Arismendi Jan 19 '11 at 04:59
  • I just checked it on my server and it is in fact redirecting to domain.com:1443 as expected.... Here is what I added to the Redirect URL: https://{HTTP_HOST}:1443/{R:1} – Dscoduc Jan 19 '11 at 23:47
  • The main problem is the variable {HTTP_HOST} which already contains the PORT information. – ishakkulekci Apr 06 '22 at 10:31