1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

网站启用HTTPS后让所有HTTP请求跳转到HTTPS

本帖由 laogui2017-05-02 发布。版面名称:主机讨论

标签:
  1. laogui

    laogui Administrator
    管理成员

    注册:
    2005-08-30
    帖子:
    15,216
    赞:
    35
    Apache
    代码:
    RewriteEngine On
    # redirect all non-www to https www
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://www.%1/$1 [L,R=301]
    # redirect http www to https www
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    
    Nginx
    代码:
    server {
      listen [::]:80;
      listen 80;
    
      server_name yourdomain.com www.yourdomain.com;
    
      # redirect http to https www
      return 301 https://www.yourdomain.com$request_uri;
    }
    
    server {
      listen [::]:443 ssl http2;
      listen 443 ssl http2;
    
      server_name yourdomain.com;
    
      # SSL code
    
      # redirect https non-www to https www
      return 301 https://www.yourdomain.com$request_uri;
    }
    
    server {
    
      listen [::]:443 ssl http2;
      listen 443 ssl http2;
    
      server_name www.yourdomain.com;
    
      # SSL code
      # other code
    }
    
     
    #1 laogui, 2017-05-02
    最后编辑: 2017-05-03
  2. zmnet

    zmnet Well-Known Member

    注册:
    2006-05-03
    帖子:
    7,809
    赞:
    62
    可以将这个和 网址始终带/去掉www的规则结合在一起。