使用https重写网址

时间:2014-02-19 10:31:23

标签: c# asp.net url url-rewriting asp.net-4.0

我有以下网址

http://www.mywebsite.com/home

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

当用户输入上述网址时,我们加载

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

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

问题是,如果用户输入https://www.mywebsite.com/home,则需要重定向到http://www.mywebsite.com/home。只需将https更改为http,因为它不受保护

同样,如果用户输入http://www.mywebsite.com/secure/reports,我们需要redurect以保护https://www.mywebsite.com/secure/reports

2 个答案:

答案 0 :(得分:1)

请参阅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);
   }
}

答案 1 :(得分:0)

更改您的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>

更多参考资料: 选中http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/

相关问题