如何实现http自动跳转到https?相信有很多朋友申请下来了SSL证书,但不知道如何部署证书,尤其是对于第一次部署SSL证书的用户,实在是个痛苦的过程。今天要跟大家说的是在部署网SSL证书后,如何把http自动跳转到https?
如何实现http自动跳转到https?国内现在主流的Web服务器无外乎这几种,Tomcat、Apache、Nginx,相信站长们都应该或多或少接触过。下面,就跟着小编一起来看看这三者分别要怎么做,如何实现自动跳转到https?
我们需要找到Apache的配置文件httpd.conf,然后添加以下代码:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
以上代码是针对整站进行跳转,如果只需要跳转某个目录,则添加一下代码:
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
在配置80端口的文件中,添加以下代码:
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root html;
index index.html index.htm;
}
这是三种服务器里面相对比较麻烦的,不过一步一步来,也是可以实现的。
首先,我们需要在服务器根目录下找到conf这个目录,找到其中server.xml文件这个文件,修改里面的redirectPort值为443,默认值一般为8443。
然后,还是在这个目录下找到web.xml文件,在尾部添加一下代码
<security-constraint>
<display-name>Auth</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/user/*</url-pattern>
<url-pattern>/main/index</url-pattern>
</web-resource-collection>
<user-data-constraint>
<description>SSL required</description>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
至此,三种服务器都可以实现http自动跳转到https,实现全站、局部、单页面的https化。