当前位置:文档之家› 强制http跳转https页面

强制http跳转https页面

she强制http跳转https页面简介https在现在网络中是公认的安全的访问方式,但是在访问的过程中很多站点是没有强制https访问的,这样会导致有些客户可以通过http访问,从而对服务器造成一些不安全因素。

下文将介绍几种强制https访问的方法。

一,wordpress中设置https强制访问。

默认登录wordpress的后台是使用http协议,这协议是明文发送的。

这可能会导致你的用户和密码被窃听。

如果使用ssl登录,这种情况则可以避免。

下面是设置方法。

1、设置wp-config.php/* That's all, stop editing! Happy blogging. */...require_once(ABSPATH . 'wp-settings.php');2、在上面的代码之前加上如下代码:define('FORCE_SSL_LOGIN', true);define('FORCE_SSL_ADMIN', true);define(‘FORCE_SSL_LOGIN’, true);#是启用登录时使用ssldefine(‘FORCE_SSL_ADMIN’, true);#后台管理也使用ssl如果担心后台使用ssl影响速度,可以不用。

但还是建议使用,因为这将有可能cookie 被劫持导致黑客伪造cookie登录后台。

wordpress只需要几步设定就可以启用强制使用HTTPS登入后端管理界面首先到wp-config.php中找到下面这段:if ( !defined(‘ABSPATH’) )define(‘ABSPATH’, dirname(__FILE__) . ‘/’);找到它以后,請在它上面加入下列此行:define(‘FORCE_SSL_LOGIN’, true);接下來请在VHOST上设定好SSL的相关设置..这里就不再叙述了以下附上nginx上如何使用vhost并且自动rewrite url跳转到https管理页面的设定值:请在rewrite rule上面加入rewrite ^.*/wp-admin(.*)https://wp.littlecho.tw/wp-admin$1 last; 即可例如下:location / {try_files $uri $uri/ /index.php;rewrite ^.*/wp-admin(.*) https://wp.littlecho.tw/wp-admin$1 last;}设定完毕后当输入http://xxxx.domain.tld/wp-admin时就会自动跳转了。

二,tomcat中的跳转配置在tomcat中设置强制https的方法有2种,下面分别介绍下这2种方法的配置:1,调整web.xml文件,将HTTP使用的server.xml文件重定向端口在记事本中打开 SGMS4\Tomcat\webapps\sgms\WEB-INF\web.xml文件,在web.xml的文件末尾的前一句添加以下内容:</servlet-mapping><error-page><error-code>404</error-code><location>/error_404.jsp</location></error-page><security-constraint><web-resource-collection><web-resource-name>Tomcat</web-resource-name><url-pattern>/*</url-pattern></web-resource-collection><user-data-constraint><transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint></security-constraint></web-app>2,调整server.xml文件以便重定向端口443。

这样做你需要更改server.xml文件,在记事本中打开 SGMS4\Tomcat\conf\server.xml文件,重定向443端口:<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 --><!-- CoyoteConnector - do not delete this line, used for detecting the next line for port information in the GMS installer --><Connector port="80" minProcessors="5" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true"/>重启服务,这个时候就会实现访问HTTP时,使用HTTPS安全协议了。

三,在JSP代码的页面中的配置jsp代码:String scheme=request.getScheme();String url=request.getRequestURI();if(!"HTTPS".equalsIgnoreCase(scheme)){response.sendRedirect("https://"+url);return ;}使用的Web 站点或虚拟服务器不使用HTTP/1.1 主机标题进行名称解析三,要使用ASP 强制SSL,请执行下列步骤:1.单击开始——运行,输入Notepad,然后单击“确定”。

2.将下列代码粘贴到空白“记事本”文档中。

在文件菜单上,单击“另存为”,然后将您的Web 服务器的根目录下的以下代码另存为名为 ForceSSL.inc 的包含文件:<%If Request.ServerVariables("SERVER_PORT")=80 ThenDim strSecureURLstrSecureURL = "https://"strSecureURL = strSecureURL & Request.ServerVariables("SERVER_NAME")strSecureURL = strSecureURL & Request.ServerVariables("URL")Response.Redirect strSecureURLEnd If%>3.对于需要SSL 的每个页面,请将下列代码粘贴到页面的顶部,以引用上一步中的包含文件:<%@Language="VBSCRIPT"%><!--#include virtual="/ForceSSL.inc"-->浏览每个页面后,包含文件中包含的ASP 代码会检测端口以确定是否使用了HTTP。

如果使用了HTTP,将使用HTTPS 将浏览器重定向到相同页面。

四,在php页面中设置https自动跳转http直接跳转为https,重定向一下就可以了。

用php就更简单了,添加header:<?phpheader("Location:https://");?>当访问http 时,跳https:<?php//将http转化为httpsif ($_SERVER["HTTPS"] <> "on"){$xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];header("Location: ".$xredir);}?>当访问https 时,跳http:<?php//将https转化为httpif ($_SERVER["HTTPS"] == "on"){$xredir="http://".$_SERVER["SERVER_NAME"]. $_SERVER["REQUEST_URI"];header("Location: ".$xredir);}?>在网页开头添加上面代码即可达到在访问http的时候自动跳转到https的效果了,从而确保访问方式一直都是安全的。

五,html代码配置在html内容中添加一下内容,实现跳转:<script language="JavaScript">window.location = "https:///index.php";</script><script language="JavaScript" type="text/JavaScript">function redirect(){var loc = location.href.split(':');if(loc[0]=='http'){location.href='https:'+loc[1];}}onload=redirect</script>这样在访问http的时候就会自动跳转到指定的https的地址了,从而确实HTTPS安全协议可以安全的包含您的网页通讯。

相关主题