如何设置http到https的自动跳转

如题所述

一、Apache服务器

我们需要找到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]

二、Nginx服务器

在配置80端口的文件中,添加以下代码:

server {

listen 80;

server_name localhost;

rewrite ^(.*)$ https://$host$1 permanent;

location / {

root html;

index index.html index.htm;

}

三、Tomcat服务器

这是三种服务器里面相对比较麻烦的,不过一步一步来,也是可以实现的。

首先,我们需要在服务器根目录下找到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>

如果用户遇到的问题不能解决,可通过wosign官网客服寻求帮助,wosign可提供免费一对一的ssl证书技术部署支持网页链接,免除后顾之忧。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-08-12
如何设置http自动跳转到https?apache环境下,配置好https后,需要设置url重定向规则,使网站页面的http访问都自动转到https访问。
1、先打开url重定向支持
1)打开Apache/conf/httpd.conf,找到 #LoadModule rewrite_module modules/mod_rewrite.so 去掉#号。
2)找到你网站目录的<Directory>段,比如我的网站目录是c:/www,找到
<Directory “C:/www”>

</Directory>
修改其中的 AllowOverride None 为 AllowOverride All3)重启apache服务2、设置重定向规则
1)在你网站目录下放一个.htaccess文件。windows环境下,不能把文件直接改名为.htaccess,会提示你必须输入文件名。所以我们先新建一个“新建文本文档.txt”文档,记事本打开,选择另存为,保存类型选择“所有文件(*.*)”,文件名输入“.htaccess”,保存。这样便生成了一个.htaccess文件。

2)编辑器打开.htaccess文件,写入如下规则:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/tz.php
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

解释:
%{SERVER_PORT} —— 访问端口
%{REQUEST_URI} —— 比如如果url是 http://localhost/tz.php,则是指 /tz.php
%{SERVER_NAME} —— 比如如果url是 http://localhost/tz.php,则是指 localhost

以上规则的意思是,如果访问的url的端口不是443,且访问页面不是tz.php,则应用RewriteRule这条规则。这样便实现了:访问了
http://localhost/index.php 或者 http://localhost/admin/index.php
等页面的时候会自动跳转到 https://localhost/index.php 或者
https://localhost/admin/index.php,但是访问 http://localhost/tz.php
的时候就不会做任何跳转,也就是说 http://localhost/tz.phphttps://localhost/tz.php
两个地址都可以访问。本回答被网友采纳
第2个回答  2016-10-19