前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >DTcms4/5中使用HttpModule将http访问301重定向到https

DTcms4/5中使用HttpModule将http访问301重定向到https

作者头像
崔文远TroyCui
发布2019-02-26 11:30:52
1.5K0
发布2019-02-26 11:30:52
举报
文章被收录于专栏:远在上海

前面虽然讲过SSL在IIS开启的几种方式,比较常见的Microsoft URL Rewrite Module修改Web.Config如下:

代码语言:javascript
复制
<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跳转,也可直接跳转。

代码如下:

代码语言:javascript
复制
//开启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访问结束
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年4月2日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档