简体   繁体   中英

Url rewriting with https

I have the below urls

http://www.mywebsite.com/home

https://www.mywebsite.com/secure/reports/

When user enters the above url we load

http://www.mywebsite.com/home.aspx

https://www.mywebsite.com/secure/reports.aspx

The problem is, if the user enter like https://www.mywebsite.com/home , need to redirect to http://www.mywebsite.com/home . Just remove change https to http, since it is not secured

Similarly, If user enters http://www.mywebsite.com/secure/reports , we need to redurect to secure https://www.mywebsite.com/secure/reports

see this :

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

Make changes to your Web.Config

<rule name="Redirect 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>

More references: Check- http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM