http to https redirect not working

I would like to redirect my page from http to https protocol but am having difficulties. Here is what I've tried so far:

Adding the following to global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (!Request.IsLocal && !Request.IsSecureConnection)
        if (HttpContext.Current.Request.IsSecureConnection || HttpContext.Current.Request.IsLocal)
            Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
}

Adding the following to every page

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    if (!IsPostBack)
        RedirectAccordingToRequiresSSL();
}

private void RedirectAccordingToRequiresSSL()
{
    if (!Request.IsLocal && !Request.IsSecureConnection)
        if (!Request.IsSecureConnection) // Need to redirect to https
            RedirectAccordingToRequiresSSL(Uri.UriSchemeHttps);
}

private void RedirectAccordingToRequiresSSL(string scheme)
{
    var url = scheme + Uri.SchemeDelimiter + Request.Url.Authority + Request.Url.PathAndQuery;
    Response.Redirect(url, false);
}

Adding a rewrite to web.config, but I think rewrite element itself is not working properly

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$"/>
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
      </rules>
    </rewrite>

No matter what I try, entering in http://my.website.here will never redirect to https://my.website.here. Error I keep getting is 403 (forbidden). Typing the URL in directly with https however works great. Any help would be greatly appreciated.

Answers


I just tried locally and I can get a redirect from http to https with the following rule.

enter code here
<rule name="TO SSL" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="Off" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{URL}" />
</rule>

Example Fiddler Trace

GET http://www.site.com/test?qs=1 HTTP/1.1
Host: www.site.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36


HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: https://www.site.com/test?qs=1
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 12 Feb 2014 01:14:49 GMT
Content-Length: 154

Need Your Help

AngularJS: Why not write logic in controller?

angularjs angularjs-directive standards angularjs-controller

Pardon me if this sounds stupid but I have been using AngularJS for a while now and everywhere I have seen people telling me to wrap my logic in a directive(or service ?) instead of my controller and

Apache Mod_Rewrite Multiple Levels

php apache .htaccess mod-rewrite

My goal is to make some friendly URL's using .htaccess. I need a mod_rewrite (for .htaccess) solution that allows me to have URLs like: