Web Connection
Redirect to HTTPS rewrite rule adjustment for virtual's web.config
Gravatar is a globally recognized avatar based on your email address. Redirect to HTTPS rewrite rule adjustment for virtual's web.config
  Mike McDonald
  All
  Jan 18, 2019 @ 05:26pm

Rick -

The Create New Project feature can create a new virtual in IIS and it establishes a default web.config file for that virtual. Included in there are (commented out) rewrite rules for "Extensionless Urls" and "Redirect to HTTPS".

The Redirect to HTTPS rule has the following line for the action..

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

The "/{R:1}" portion doesn't work quite right when placed in the virtual's web.config file, because a link such as..

http://www.site.com/virtual/page.wc

..gets redirected to..

https://www.site.com/page.wc

..with the "/virtual" portion removed.

The /{R:1} syntax works if this rewrite rule in placed in the web.config of the website root, but not when placed in the virtual's config.

I've found that the following syntax works for when the rewrite rule exists in the virtual's web.config file..

<action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="SeeOther" />

The {URL} syntax does include any query string parameters, and the initial slash character.

Also note that the "Redirect to HTTPS" rule should come before the "Extensionless Urls" rule. Otherwise a link like http://www.site.com/virtual/page will go ahead and fire the OnRewriteHandler method without redirecting to https first.

- Mike McDonald

Gravatar is a globally recognized avatar based on your email address. re: Redirect to HTTPS rewrite rule adjustment for virtual's web.config
  Rick Strahl
  Mike McDonald
  Jan 18, 2019 @ 06:48pm

Good stuff. Added and create the following in the template:

<rules>

  <rule name="Redirect to HTTPS" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="SeeOther" />
  </rule>

  <rule name="Extensionless Urls" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^((?!\.).)*$" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="UrlRewriteHandler.<%= pcScriptMap %>" appendQueryString="true" />
  </rule>

</rules>
</rewrite>

This seems to work seamless either with IIS in a virtual or IIS Express (assuming the appropriate certs are installed).

Also double checked the URLRewriteHandler logic and updated the docs - there were a few inconsistencies there as well.

+++ Rick ---

© 1996-2024