20

Whenever someone makes request over HTTP protocol I rewrite the url to make it HTTPS. This is the code in web.config:

<rule name="Imported Rule 1-1" enabled="true" stopProcessing="true">
    <match url="^(?!https://).*" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
        <add input="{SERVER_PORT}" pattern="80" ignoreCase="false" />
    </conditions>
    <action type="Rewrite" url="https://abc.com/{R:1}" />
</rule> 

However when I browse on http:// I get IIS error

HTTP Error 500.50 - URL Rewrite Module Error. The expression "https://abc.com/{R:1}" cannot be expanded.

How can I resolve this? I am utterly confused.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
TCM
  • 16,780
  • 43
  • 156
  • 254

2 Answers2

21

The matches are zero based.

<action type="Rewrite" url="https://abc.com/{R:1}" />

Won't work because you only have one match. You need:

<action type="Rewrite" url="https://abc.com/{R:0}" />

Also, this won't work, because you can only match on the path below the site root.

<match url="^(?!https://).*" ignoreCase="false" />

It looks like you are checking for ssl. Try this instead:

      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
SouthShoreAK
  • 4,176
  • 2
  • 26
  • 48
  • 2
    While the matches are zero-based, the first match `{R:0}` *always* contains the full string matched, and the second match `{R:1}` matches the *first* capture group within `{R:0}`. So, this answer is incorrect in the first part. You can see for yourself in the "Test Pattern" dialog. – jpaugh Aug 18 '20 at 21:02
  • @jpaugh I wonder if it changed in the 10 years since I wrote the answer? I know it worked when I wrote it because I had just solved exactly the problem OP was having. – SouthShoreAK Aug 16 '22 at 15:22
  • This convention matches [PCRE](https://stackoverflow.com/tags/pcre), so I can't imagine that it changed. It's also possible the OP corrected it without a second thought. – jpaugh Jan 18 '23 at 17:39
-2

You can redirect through web config to Hope it will help full

<rule name="Redirect to WWW" stopProcessing="true">
  <match url=".*" />
     <conditions>
       <add input="{HTTP_HOST}" pattern="^abc.com$" />
     </conditions>
  <action type="Redirect" url="http://www.abc.com/{R:0}" redirectType="Permanent" />
</rule>
Janak Prajapati
  • 896
  • 1
  • 9
  • 36