Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to convert https to http and viceversa in IIS 7.5?
I have internal network url for a certain users and external url for few others. HTTPS was enabled by configuring SSL certificate for the same. But if the user changes
https://www.x.com must automatically redirect to to http://www.x.com. Similarly in internal url it must change https://192.168.4.1. to http://192.168.4.1 to avoid certificate error internally.

I saw this

XML
<rule name="ForceHttpsBilling" stopProcessing="true">
  <match url="(.*)billing/(.*)" ignoreCase="true" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="false" />
  </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>

<!-- https:// to http:// rule -->
<rule name="ForceNonHttps" stopProcessing="true">
  <match url="(.*)billing/(.*)" ignoreCase="true" negate="true" />
  <conditions>
      <add input="{SERVER_PORT}" pattern="^443$" />
  </conditions>
  <action type="Redirect" redirectType="Found" url="http://{HTTP_HOST}{REQUEST_URI}" />
</rule>


What all should be installed. Also what should be set in HTTP_HOST and REQUEST_URL .
Posted

1 solution

I normally use something like this and it has always worked for me.
You can have a function like this in global.asax and can call in Application_BeginRequest

private void RedirectToCorrectSSLScheme()
{
Uri pageRequest = Request.Url;
string requestPath = pageRequest.GetLeftPart(UriPartial.Path).ToLower();

requestPath = Server.UrlDecode(requestPath);
// PageIsSecure returns if the given page should be secure or not. I
//maintain a list of secure pages or
//secure directory in an XML config.
bool securePage = GetSecurePages().PageIsSecure(requestPath);
if (pageRequest.Scheme == "https" && !securePage && requestPath.Contains(".aspx"))
{
Response.Redirect("http://" + pageRequest.Host + pageRequest.PathAndQuery, true);
}
else if (pageRequest.Scheme == "http" && securePage && requestPath.Contains(".aspx"))
{
Response.Redirect("https://" + pageRequest.Host + pageRequest.PathAndQuery, true);
}
}
 
Share this answer
 
Comments
Rahul 105 29-Jan-14 6:17am    
I tried by checking the internal url content and external url content in FOLLOWING way. Will it be fine?? My code as follows:

private void RedirectToCorrectSSLScheme()
{
Uri pageRequest = Request.Url;
string requestPath = pageRequest.GetLeftPart(UriPartial.Path).ToLower();

requestPath = Server.UrlDecode(requestPath);
// PageIsSecure returns if the given page should be secure or not. I
//maintain a list of secure pages or
//secure directory in an XML config.
// bool securePage = GetSecurePages().PageIsSecure(requestPath);
//Instead of localhost we can set the IP Address of Qatar server
if (pageRequest.Scheme == "https" && pageRequest.Host.Contains("localhost") && requestPath.Contains(".aspx"))
{
//Response.Redirect("http://" + pageRequest.Host + pageRequest.PathAndQuery, true);
Response.Redirect(requestPath, true);
}
//Added by Alex to check external url of qatar server
//Instead of localhost we can set the IP Address of Qatar server
if (pageRequest.Scheme == "http" && pageRequest.Host.ToString()!="localhost" && requestPath.Contains(".aspx"))

{
//Response.Redirect("https://" + pageRequest.Host + pageRequest.PathAndQuery, true);

Response.Redirect(requestPath, true);
}
}
void Application_BeginRequest(object sender, EventArgs e)
{
//Function Added by Alex on Jan 29,2014 to clear http/https issue under Qatar Server
RedirectToCorrectSSLScheme();

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900