Link to home
Create AccountLog in
Avatar of trs28
trs28

asked on

IIS URL Rewrite HTTP to HTTPS w/ Port on Specific Domain

Hello!  I have a snipped of code that I use in the web.config file of an Azure project to redirect all HTTP requests to HTTPS.  It does so quite gracefully too I might add.

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

Open in new window


However, there are two different domains that could end up having this code applied to it.  For example, we'll say it's www.standardport.com & www.customport.com.   I'm not sure if they're real addresses, but if they are, they're not affiliated with me in any way.   Okay ... so if www.standardport.com is entered, I need the code to run just as it's listed above and simply redirect the URL to HTTPS.   If www.customport.com is entered, I still need it to redirect to HTTPS but also to port 555.  

I know it can be accomplished, but I've been playing with different configurations and can't seem to get the right combination of code to make it work.   Any help is very much appreciated!
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

The normal way would be to make it part of the server name like "https://www.customport.com:555/".  Does that work?  I'm assuming that putting that in your address bar actually goes to "https://www.customport.com/".  If it doesn't work in the address bar, I'm not sure it will work at all.
Avatar of trs28
trs28

ASKER

That's what I'm trying to do ... I'm trying to append the custom port number to the URL, BUT it can only be for the one domain.  So .....

If the user types in http://www.customport.com, the redirected address will be https://www.customport.com:555.

If the user types in http://www.standardport.com, the redirected address will be
https://www.standardport.com.

I already have the HTTP -> HTTPS redirection handled.  I just don't know how to quite manipulate it to have it pick up the specific URL and append the port number only to that URL.
I just realized that {SERVER_NAME} is the server variable.  I don't know how you would change that to add the port.  And I don't know if you can change the web.config file with a program.  You may have to first redirect to an ASP/ASPX page that can 'read' the {SERVER_NAME} variable and do a redirect based on which site is being asked for.
Avatar of trs28

ASKER

Well I just read where using {HTTP_HOST} instead of {SERVER_NAME} might not work as smoothly.... I found another snippet on another site that redirects the port numbers like I'm looking for, but it has no code to only do it for one domain....
This is what I meant though it may not 100% accurate.  From this page http://msdn.microsoft.com/en-us/library/540y83hx.aspx and this page http://msdn.microsoft.com/en-us/library/ms525396%28v=vs.90%29.aspx :

Dim Svrname
Response.BufferOutput = True
Svrname = Request.ServerVariables("SERVER_NAME")
If Svrname = "www.standardport.com" Then
    Response.Redirect("https://www.standardport.com/")
ElseIf Svrname = "www.customport.com" Then
    Response.Redirect("https://www.customport.com:555/")
End If

Open in new window

Avatar of trs28

ASKER

I'll have to create a C# equivalent, but tell me, where the code should this be inserted?  Again, it's an ASP.NET Windows Azure project.   I'm a program manager full time w/ a respectable amount of coding experience.  It's been 4 years since I've done any hardcore development and frankly, these web technologies are one thing that's somewhat foreign to me.  

I was anticipating that to go in the web.config file, but I don't have any problem handling it in code if it provides me with a solution.  I just want to make sure I'm putting it in the proper place first ....
Avatar of trs28

ASKER

Actually, I just realized that handling it in code isn't going to work.   I need this to happen in the web.config file b/c the one site will be unreachable due to the custom port number if handled in code.  

By making the change through rewriting IIS rules, all that work is handled prior to accessing the site.
Here's the IIS page on redirect: http://www.iis.net/ConfigReference/system.webServer/httpRedirect 

You have two problems.  'web.config' is an XML file with no programing ability, no 'if' statments or anything like that.  Second, 'web.config' is 'run' before anything else happens.  

I don't think you can do what you want in a single 'web.config' file.  I'm curious why two different URLs are ending up at the same place using the same 'web.config' file.  If you could split them, you wouldn't have this problem.
Avatar of trs28

ASKER

I appreciate the link, but none of that is going to help me.... there's very little information on that page that apply to what I need to do.  

In regards to my "problems," well, frankly, they're not problems at all.   Call me crazy, but last I checked, IIS rewrite rules have nothing to do with if statements or anything like that.  

Your last paragraph is pretty much incorrect too.   I've seen the SSL redirection firsthand since I have that part working.  I've seen sample scripts that append a port number to a URL (but not just one specific URL only).   I've also seen scripts that react conditionally based off of other factors that don't quite apply.    ...and all of these have been in the form of IIS rewrite rules.

My real problem is that I need a combination of all of those examples working together to generate the redirect URL and can't seem to get it formatted properly.  

In regards to the "if you could split them" comment, they were previously split into two separate projects.  Now they're an integrated solution on Azure which provides me massive advantages over the former.  In fact, the solution may end up being a combination of 10+ sites one day so I have no intention on regressing.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer