前面虽然讲过SSL在IIS开启的几种方式,比较常见的Microsoft URL Rewrite Module修改Web.Config如下:
<system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite> </system.webServer>
但在DTcms中,本身已经用了自定义的HttpModule,可直接利用其写法来方便地实现支持。打开Web.UI下的HttpModule.cs,增加如下判断,可选择301跳转,也可直接跳转。
代码如下:
//开启SSL访问开始 string oldUrl = ((HttpApplication)sender).Request.Url.ToString(); if (!oldUrl.StartsWith("https://")) { string newUrl = oldUrl.Replace("http://", "https://"); //301重定向 ((HttpApplication)sender).Response.StatusCode = 301; ((HttpApplication)sender).Response.AddHeader("Location", newUrl); ((HttpApplication)sender).Response.End(); //直接重定向 //((HttpApplication)sender).Context.RewritePath(newUrl); } //开启SSL访问结束