0

I am using the Rewrite tool for ASP.NET to redirect from http to hpps. I want to reroute to

https://services.net/ExitInterview/home/about

But currently it is routing to

https://services.net/home/about

Below is my redirect rule:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
          redirectType="Permanent" />
    </rule>`

Can I mix the "HTTP_HOST" text with hard-coded text in the rule string? Or is there another way?

I don't want to hard code the url because it changes with local, staging, and production.

Steve Scott
  • 1,441
  • 3
  • 20
  • 30

2 Answers2

1
<rule name="HTTP to HTTPS redirect" ="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/ExitInterview/{R:1}"
      redirectType="Permanent" />
</rule>

Give this a try

Mohsin Mehmood
  • 4,156
  • 2
  • 12
  • 18
  • Well that was easy. Adding ExitInterview, I thought I tried this early on and it didn't work. Now it does work. maybe I didn't refresh the cache the previous time. I refreshed the cache and this worked. Thank you! – Steve Scott May 11 '18 at 18:39
0

This should do what you want in terms of redirecting to HTTPS from HTTP in a web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
    <httpRuntime executionTimeout="180" />
</system.web>
<system.webServer>
    <httpErrors errorMode="Detailed" existingResponse="PassThrough" />
    <rewrite>
    <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>
    </rules>
    </rewrite>
</system.webServer>
</configuration>

I use this exact snippet for a webserver that enforces HTTPS redirection, but that also seems to be pretty close to what you have. Are you sure you have configured the structure of the web.config file correctly? - I remember running into issues when I would leave out something.

ViaTech
  • 2,143
  • 1
  • 16
  • 51
  • I am not sure what you mean about the "structure of the web.config file". – Steve Scott May 11 '18 at 17:35
  • Where does HTTP_HOST value come from? Maybe it needs to be set correctly? – Steve Scott May 11 '18 at 17:35
  • Well on a Windows Webserver,(if the rewrite rule is defined on the site level) then you need to place this information in a web.config file (like I have edited above), and place it in your root directory. HTTP_HOST is a server variable that is essentially the host domain: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference – ViaTech May 11 '18 at 17:41
  • Yes, I am working with the web.config file. The redirect is redirecting to the host. The problem is I need it to redirect to a subdomain of the host, "ExitInterview". – Steve Scott May 11 '18 at 17:43
  • Try changing {R:1} to {R:0} (This could be your issue) : https://stackoverflow.com/questions/16998832/iis-url-rewrite-rn-clarification – ViaTech May 11 '18 at 17:50
  • Thanks @ViaTech but that was not it. – Steve Scott May 11 '18 at 17:59
  • Hmm... Can you provide a live site where the redirection fails? This seems hard to answer without a reference to the live issue if you have tested my above web.config... For example using my snippet above, I can go to any sub domain of my company's site and the redirect is accurate (i.e. http://www.sample.com/one/two/three/index.php), would just go to HTTPS, I use PHP because that is what I have tested, but it shouldn't matter PHP or ASPX – ViaTech May 11 '18 at 19:43
  • What is the difference between R:1 and R:0? – Steve Scott May 13 '18 at 20:59