安装SSL证书后实现Http自动跳转到Https教程

Apache版本

如果需要整站跳转,则在网站的配置文件的标签内,键入以下内容:

 

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

如果对某个目录做强制跳转,则使用以下代码:

  1. RewriteEngine on
  2. RewriteBase /yourfolder
  3. RewriteCond %{SERVER_PORT} !^443$
  4. #RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
  5. RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Nginx版本

在配置80端口的文件里面,写入以下内容即可。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. rewrite ^(.*)$ https://$host$1 permanent;
  5. location / {
  6. root html;
  7. index index.html index.htm;
  8. }

PHP页面跳转:添加在网站php页面内

  1. if ($_SERVER["HTTPS"] <> "on")
  2. {
  3. $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  4. header("Location: ".$xredir);
  5. }

单独页面通用代码段:较适合指定某一个子页单独https 在需要强制为https的页面上加入以下代码进行处理http—>https

  1. <script language="JavaScript" type="text/JavaScript">
  2. function redirect()
  3. {
  4. var loc = location.href.split(':');
  5. if(loc[0]=='http')
  6. {
  7. location.href='https:'+loc[1];
  8. }
  9. }
  10. onload=redirect
  11. </script>
分享到:
上一篇
Nginx 证书部署
下一篇
Linux服务器上生成CSR和KEY
服务中心
客服
在线客服
24小时为您服务
咨询
联系我们
联系我们,为您的业务提供专属服务。
24/7 技术支持
如果您遇到寻求进一步的帮助,请过工单与我们进行联系。
24/7 即时支持
芒竹智能云
芒竹智能云
售后客服
评价
您对当前页面的整体感受是否满意?
😞
非常不满意
😕
不满意
😐
一般
🙂
满意
😊
非常满意