Daniel Anderson Daniel Anderson - 1 month ago 7
ASP.NET (C#) Question

HTTPS Redirect not working quite right

I'm using the following code in my ASP.NET web.config file to redirect "www" requests to my secure web site:

<rewrite>
<rules>
<clear/>
<rule name="httpsredirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{REQUEST_URI}" />
</rule>
</rules>
</rewrite>


The goal is to ensure that when someone enters


"http://www.example.com/somepage.aspx"


they get redirected to


"https://www.example.com/somepage.aspx"


.

The problem is that the redirect works just fine in sending them to the secure site, but the destination URI is missing, so they get sent to the site's root default page. Any help here? Thanks!
Right now

Answer

Try changing

<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{REQUEST_URI}" />

To

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