全球主机交流论坛

标题: 如何让http自动跳转到https [打印本页]

作者: 天蚕土豆    时间: 2018-3-17 13:47
标题: 如何让http自动跳转到https
我的域名已经申请了ssl,那么在虚拟空间里面如何设置,才可以让http自动跳转到https呢?谢谢
作者: cnmeimei    时间: 2018-3-17 13:49
301
301
作者: naiji    时间: 2018-3-17 13:57
apache虚拟主机的话修改 .htaccess:
  1. <IfModule mod_rewrite.c>
  2.     RewriteEngine On

  3.     RewriteCond %{HTTPS} off
  4.     RewriteRule ^(.*)$ http s://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  5. </IfModule>
复制代码


http s去掉空格
作者: 我不是你的    时间: 2018-3-17 14:53
naiji 发表于 2018-3-17 13:57
apache虚拟主机的话修改 .htaccess:

我比较喜欢用nginx
作者: imes    时间: 2018-3-17 14:59
提示: 作者被禁止或删除 内容自动屏蔽
作者: 天蚕土豆    时间: 2018-3-17 15:09
naiji 发表于 2018-3-17 13:57
apache虚拟主机的话修改 .htaccess:

是放到 public_html 目录吗
作者: naiji    时间: 2018-3-17 15:17
你要全站跳转的话就放在public_html
作者: doio    时间: 2018-3-17 15:27
启用 HSTS
这是我的部分 nginx 配置
  1.         if ($scheme != "https") {

  2.                 return 301 https://$host$request_uri;
  3.                 }
复制代码



提取自 certbot

然后加了这个 header:
  1. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
复制代码

作者: 花猫    时间: 2018-3-17 15:33
元老问这样的问题合适吗?
作者: 天蚕土豆    时间: 2018-3-17 15:37
naiji 发表于 2018-3-17 15:17
你要全站跳转的话就放在public_html

成功,谢谢你
作者: Giine    时间: 2018-3-17 15:48
  1. server {
  2.         server_name gov.cn;
  3.         listen   443 ssl http2;
  4.         ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
  5.             ssl_certificate /etc/letsencrypt/live/gov.cn/fullchain.pem; # managed by Certbot
  6.             ssl_certificate_key /etc/letsencrypt/live/gov.cn/privkey.pem; # managed by Certbot

  7.         ssl_session_cache        shared:SSL:10m;
  8.         ssl_session_timeout      60m;
  9.         ssl_session_tickets      on;
  10.         ssl_stapling             on;
  11.         ssl_stapling_verify      on;
  12.         resolver                 8.8.4.4 8.8.8.8  valid=300s;
  13.         resolver_timeout         10s;

  14.         root /var/www/gov/public;
  15.         index index.html index.php index.htm;

  16.         location / {
  17.                 try_files $uri $uri/ /index.php?$args;
  18.         }

  19.         client_max_body_size    4m;

  20.         access_log /var/log/nginx/elsenow.log;
  21.         error_log /var/log/nginx/error.elsenow.log;

  22.         error_page 404 /404.html;
  23.         error_page 500 502 503 504 /50x.html;
  24.         location = /50x.html {
  25.                 root /usr/share/nginx/html;
  26.         }
  27.         location ~ \.php$ {
  28.                 try_files $uri /index.php =404;
  29.                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
  30.                 fastcgi_pass 127.0.0.1:9000;
  31.                 fastcgi_index index.php;
  32.                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  33.                 include fastcgi_params;
  34.         }

  35. }

  36. server {
  37.         listen 80;
  38.         server_name gov.cn www.gov.cn;
  39.         location / {
  40.                 rewrite ^/(.*)$ https://gov.cn/$1 permanent;
  41.         }
  42. }
复制代码





欢迎光临 全球主机交流论坛 (https://loc.mjj8.eu.org/) Powered by Discuz! X3.4