We have a website and some of the pages are using `https` and those pages kept in `magic` folder.

https enabling and port number is configured in web.config for a site.

However if the user trying to access magic folder contents using `http`, we need to redirect back to **https** and vise versa

***Ex Case 1:*** Working for `http to https`

`http://mysite/magic-look` to `https://mysite/magic-look`

Here, we used 

```
<urlMappings>
  <add url="~/magic-look" mappedUrl="~/magic/look.aspx"/>
  <add url="~/help" mappedUrl="~/Help/default.aspx"/>

```

In Global.asax

```
protected void Application_BeginRequest(object sender, EventArgs e)
{

    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    var secPort = String.IsNullOrEmpty(ConfigurationManager.AppSettings["securePort"]) ? 0 : Convert.ToInt32(ConfigurationManager.AppSettings["securePort"]);
    var secProtocolEnabled = String.IsNullOrEmpty(ConfigurationManager.AppSettings["useSecure"]) ? false : true;

    bool isSecureUrl = (url.IndexOf("/magic/", StringComparison.OrdinalIgnoreCase) >= 0) ? true : false;

    if (url.IndexOf(".aspx", StringComparison.OrdinalIgnoreCase) >= 0)
    {
        url = url.Replace(":" + secPort, "");

        if (isSecureUrl && secProtocolEnabled)
        {

            if (HttpContext.Current.Request.Url.Port != secPort)
            {
                //change .aspx page back to original SEO friendly URL and redirect
                url = url.Replace(HttpContext.Current.Request.Url.AbsolutePath, HttpContext.Current.Request.RawUrl);                    
                HttpContext.Current.Response.Redirect(Regex.Replace(url, "http", "https", RegexOptions.IgnoreCase));
            }
        }
        else
        {

            if (HttpContext.Current.Request.Url.Port == secPort && !isSecureUrl)
            {
                //cause infinite loop
               url = url.Replace(HttpContext.Current.Request.Url.AbsolutePath, HttpContext.Current.Request.RawUrl); 
               var targetUrl = Regex.Replace(url, "https", "http", RegexOptions.IgnoreCase);                   
               HttpContext.Current.Response.Redirect(targetUrl);                        
            }                
        }            
    }
}

```

**Non https page accessed using Https, Not working, infinite loop**

***ISSUE:*** Not working for `https to http`

`https://mysite/help` to `http://mysite/help`

It gives `infinite loop.. keep redirecting to *https://mysite/help*` 

```
https://mysite/help --> 302 Found
https://mysite/Help --> 302 Found 
https://mysite/Help --> 302 Found 
https://mysite/Help --> 302 Found .............

```

**UPDATE:**

If it remove this, it works fine. 

```
  url = url.Replace(HttpContext.Current.Request.Url.AbsolutePath, 
         HttpContext.Current.Request.RawUrl);

```

But 3 requests instead of 2

```
https://mysite/help --> 302 Found
https://mysite/Help/Default.aspx--> 302 Found 
http://mysite/Help/Default.aspx--> 200 OK

```

However i want SEO friendly url like `http://mysite/Help/`

**UPDATE 2: ROOT CAUSE:**

Whenever the url is `https://../something` and redirecting to `http://../something` is always making request `https://../something`