将 http 重定向到 https 仅在页面刷新 Apache2 后有效

Redirect http to https only works after page refresh Apache2

我已经在我的网站和 example.com 上安装了 SSL 证书,一切正常,这意味着键入 example.com 会正确重定向到 https://example.com。但是,我也为子域安装了证书,这样 link 就变成了:subdomain.example.com.

我的目标是让子域。example.com 重定向到 https://subdomain.example.com。这听起来可能很奇怪,但这个半成品意味着当我第一次浏览子域时。example.com 它使用 http 协议但是当我刷新同一页面时它切换到 https 协议。

这是我的 VirtualHost conf 文件(端口 80):

<VirtualHost *:80>
  ServerName subdomain.example.com
  ServerSignature Off

  ProxyPreserveHost On

  AllowEncodedSlashes NoDecode

  <Location />
    Require all granted

    ProxyPassReverse http://127.0.0.1:8181
    ProxyPassReverse http://example.com/
  </Location>

  RewriteEngine on

  #Forward all requests to gitlab-workhorse except existing files like error documents
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads/.*
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

  # needed for downloading attachments
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public

RewriteCond %{SERVER_NAME} =subdomain.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/ [R=301,L]

</VirtualHost>

我已经从上面的示例中删除了不相关的行。这是 443 配置文件:

< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
<    IfModule mod_ssl.c>
    SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
    <VirtualHost *:443>
      ServerName subdomain.example.com
      ServerSignature Off

      ProxyPreserveHost On

      AllowEncodedSlashes NoDecode

      <Location />
        Require all granted

        #Allow forwarding to gitlab-workhorse
        ProxyPassReverse http://127.0.0.1:8181
        ProxyPassReverse http://domain/
      </Location>

      RewriteEngine on

      #Forward all requests to gitlab-workhorse except existing files like error documents
      RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
      RewriteCond %{REQUEST_URI} ^/uploads/.*
      RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

      # needed for downloading attachments
      DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public


    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/subexample.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    Header always set Strict-Transport-Security "max-age=31536000"
    SSLUseStapling on
    Header always set Content-Security-Policy upgrade-insecure-requests
    </VirtualHost>
    </IfModule>

值得注意的是,我正在使用 certbot。

希望有人能帮助我。

你说"My goal is to have subdomain.example.com redirect to https://subdomain.example.com"

那为什么要在 :80 VirtualHost 中配置所有代理?只需强制重定向到 :443,并让 :443 处理代理(和其他)。

因此您的 VirtualHost 将变为:

<VirtualHost *:80>
    ServerName subdomain.example.com
    CustomLog logs/subdomain_80_access.log combined
    ErrorLog  logs/subdomain_80_error.log

    RewriteEngine On
    RedirectMatch ^/(.*)$ https://subdomain.example.com/

</VirtualHost>