IIS与APACHE实现HTTP重定向到HTTPS

发布时间:2021-06-03 17:33:52 作者:Leah
来源:亿速云 阅读:154

这篇文章给大家介绍IIS与APACHE实现HTTP重定向到HTTPS,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="redirect to HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Apache http跳转https配置

修改.htaccess文件,在文件里增加如下几行:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

另一种写法是:

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]

nginx配置

nginx的rewrite方法

思路

这应该是大家最容易想到的方法,将所有的http请求通过rewrite重写到https上即可

配置

server { 
  listen 192.168.1.111:80; 
  server_name test.com; 
   
  rewrite ^(.*)$ https://$host$1 permanent; 
}

搭建此虚拟主机完成后,就可以将http://test.com的请求全部重写到https://test.com上了
 
nginx的497状态码

error code 497

497 - normal request was sent to HTTPS 

解释:当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码
 
思路

利用error_page命令将497状态码的链接重定向到https://test.com这个域名上

配置

server { 
    listen       192.168.1.11:443;  #ssl端口 
    listen       192.168.1.11:80;   #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口 
    server_name  test.com; 
    #为一个server{......}开启ssl支持 
    ssl                  on; 
    #指定PEM格式的证书文件  
    ssl_certificate      /etc/nginx/test.pem;  
    #指定PEM格式的私钥文件 
    ssl_certificate_key  /etc/nginx/test.key; 
     
    #让http请求重定向到https请求  
    error_page 497  https://$host$uri?$args; 

index.html刷新网页

思路
上述两种方法均会耗费服务器的资源,我们用curl访问baidu.com试一下,看百度的公司是如何实现baidu.com向www.baidu.com的跳转
 
可以看到百度很巧妙的利用meta的刷新作用,将baidu.com跳转到www.baidu.com.因此我们可以基于http://test.com的虚拟主机路径下也写一个index.html,内容就是http向https的跳转

index.html

<html> 
<meta http-equiv="refresh" content="0;url=https://test.com/"> 
</html> 

nginx虚拟主机配置

server { 
    listen 192.168.1.11:80; 
    server_name test.com; 
     
    location / { 
                #index.html放在虚拟主机监听的根目录下 
        root /srv/www/http.test.com/; 
    } 
        #将404的页面重定向到https的首页 
    error_page  404 https://test.com/; 
}  

关于IIS与APACHE实现HTTP重定向到HTTPS就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. HTTP与HTTPS简介
  2. 5 HTTP重定向HTTPS-方法2

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

http https

上一篇:Python操作Excel实现批量替换功能

下一篇:nginx location中uri的截取

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》