URL Rewrite rule not working Http to Https Redirect IIS 10 - Asp.Net Framework 4.8 - aspx - Web Forms

techthiru 40 Reputation points
2024-06-07T19:10:18.9433333+00:00

Using URL rewrite module 2.1 in IIS 10 is not working as expected.

Here is my requirement

I have several web applications running under "Default Web Site". One of them is "WebAppDemo"

The Default Web Site has bindings as in the screenshot

Bindings

The WebAppDemo running under "Default Web Site" can be accessed via this URL:

http://dev-abc-iis.myorg.com/WebAppDemo

I want to allow https: only for this WebAppDemo application.

My team wants to take an approach like this:

  • Create a new web site say "WebAppDemo Site"
  • Create a web application under this new web site say "WebAppDemo"
  • Create a https: binding (only) for this "WebAppDemo Site" on a custom port number(non standard port number)
      https:9443
      hostname: dev-abc-iis.myorg.com
      IP Address: All UnAssigned
      Require Server  Name Identification: Checked
      **Note:** The WebAppDemo under both the "Default Web Site" and "WebAppDemo Site" point to the same physical folder : inetpub\wwwroot\WebAppDemo
    

We prefer this approach such that we want the users keep requesting the old url without needing to update their bookmarks.

URL Rewrite Rules

With the above setup, I have written this rule and put it in the web.config file for the "Default Web Site" under inetpub\wwwroot\web.config

<rules>
	<rule name="WebAppDemo Https Redirect" stopProcessing="true">
		<match url="WebAppDemo(/.*)?$" />
		<conditions>
			<add input="{HTTPS}" pattern="^OFF$" />
			<add input="{HTTP_HOST}" pattern="([^/:]+)(:[^/]*)?" />
        </conditions>  			
		<action type="Redirect" url="https://{C:1}:9443/{R:0}" appendQueryString="false" />
	</rule>
</rules>

Here are the specifications of my requirements for the rules

http: requests made to WebAppDemo application running under "Default Web Site" should be redirected to the https: site which is bound to "WebAppDemo Site" -> WebAppDemo on the custom port :9443

The result of the above mentioned rule is:

When i browse this URL using a browser: http://dev-abc-iis.myorg.com/WebAppDemo

I am redirected to this URL : https://dev-abc-iis.myorg.com/WebAppDemo/

You can notice, in the redirected URL, the port number :9443 is missing and this result is not the expected result.

I am not sure what is wrong in the rule.

Somebody, please help me fix this and get this working!! Update:06/08/24

I still am gathering answers for this question. I prefer to fix this rule before i consider other options.

Thanks!

Internet Information Services
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,388 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,256 Reputation points
    2024-06-08T16:46:23.8033333+00:00

    i would like to get the rule fixed before considering alternative approaches.

    I tested the code and I believe the rule functions according your your use case. I used the wildcard pattern. Double check the port and web app name.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <clear />
                    <rule name="Port redirect path" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                        <match url="*/*" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{R:1}" pattern="WebAppDemo" />
                            <add input="{HTTPS}" pattern="OFF" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}:9443/{R:1}/{R:2}" redirectType="Found" />
                    </rule>
                    <rule name="Port redirect root" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                        <match url="*" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{R:0}" pattern="WebAppDemo" />
                            <add input="{HTTPS}" pattern="OFF" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}:9443/{R:0}" redirectType="Found" />
                    </rule>
                </rules>
            </rewrite>
            <staticContent>
                <clientCache cacheControlMode="NoControl" />
            </staticContent>
        </system.webServer>
    </configuration>
    
    
    

    I used the following reference documentation.

    https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

    Edit: You're configuration is returning 301 (Moved Permanently). Your browser can/will cache the URL. If making rule changes does not seem to changes anything then clear your browser cache.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 27,256 Reputation points
    2024-06-07T21:14:31.24+00:00

    The easiest approach is adding redirect code in the global.asax file of the web application you wish to redirect from.

    void Application_BeginRequest(Object source, EventArgs e)
    {
        Uri url =  HttpContext.Current.Request.Url;
        if(url.Port == 443)
        {
            Response.Redirect("https://dev-abc-iis.myorg.com:9443/WebAppDemo/");
        }
    }
    

    The code above does a simple 443 port number check. However, the Uri type has a lot of properties which can be used to evaluate what URL the user requested to determine where to redirect the user's browser.

    1 person found this answer helpful.