Apache 虚拟主机将 http 重定向到 https

Apache virtual host redirect http to https

我知道这个问题已经被问过和回答过很多次了。但是我似乎无法让它适用于我的场景。

我想将所有 http 流量重定向到 https,并将 https 根目录重定向到登录页面。 vhost.conf 文件中是我的虚拟主机。

<VirtualHost *:80>
    DocumentRoot /var/www
    ServerName sub-domain.mydomain.com
    ServerAdmin admin@example.com
    CustomLog /var/log/httpd/http_access.log common
    LOGFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    ErrorLog /var/log/httpd/http_error.log
    TransferLog /var/log/httpd/http_transfer.log

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteRule ^/(.*) https://%{HTTP_HOST}/
    #Redirect permanent / https://%{HTTP_HOST}%/login/mylogin.jsp (Not used as I need to use a rewrite rule rather than redirect only the root)
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /var/www
    ServerName sub-domain.mydomain.com
    ServerAdmin admin@example.com
    CustomLog /var/log/httpd/https_access.log common
    LOGFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    ErrorLog /var/log/httpd/https_error.log
    TransferLog /var/log/httpd/https_transfer.log

    SSLEngine on
    SSLOptions +StrictRequire
    SSLCertificateFile /etc/httpd/conf/mydomain.crt
    SSLCertificateKeyFile /etc/httpd/conf/mydomain.key
    SSLCertificateChainFile /etc/httpd/conf/intermediate.crt

    # HSTS (mod_headers is required) (15768000 seconds = 6 months)- ref https://mozilla.github.io/server-side-tls/ssl-config-generator/
    Header always set Strict-Transport-Security "max-age=15768000"

    JkMount .....  

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>

    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
        SSLRequireSSL
    </Directory>

    BrowserMatch "MSIE [2-5]" \
    nokeepalive ssl-unclean-shutdown \
    downgrade-1.0 force-response-1.0

    AddDefaultCharset utf-8
    AddType image/svg+xml svg svgz
    AddEncoding gzip svgz

    RewriteEngine on
    RewriteRule ^/$ /login/mylogin.jsp [R=permanent,L]
</VirtualHost>

登录重定向的 https 根目录工作正常。但是我无法使 http 到 https 重定向正常工作。 我尝试了各种建议,例如;

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^/?(.*) https://%{SERVER_NAME}/
RewriteRule ^/?(.*) https://%{SERVER_NAME}/%

根据此处的各种帖子。但是其中一些不起作用,并且在一些建议中有评论提到查询字符串不适用于 %{REQUEST_URI} 变量。

我需要一个好的解决方案,它将所有 http 流量重定向到 https,同时保留其余请求 url。 例如。

http://sub-domain.mydomain.com to https://sub-domain.mydomain.com

http://sub-domain.mydomain.com/somepage.html to https://sub-domain.mydomain.com/somepage.html

http://sub-domain.mydomain.com/thepage.html?day=tuesday&month=march to https://sub-domain.mydomain.com/thepage.html?day=tuesday&month=march

一旦我测试了并且对重定向按预期工作感到满意,我将永久重定向。

我不想使用 htaccess。

您已经处理了从“/”到“/login/mylogin.jsp”的特定重定向,因此它应该可以工作。 要将所有 http 流量重定向到 HTTPS,您可以使用 RedirectMatch。

<VirtualHost *:80>
    DocumentRoot /var/www
    ServerName sub-domain.mydomain.com
    ServerAdmin admin@example.com
    CustomLog /var/log/httpd/http_access.log common
    LOGFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    ErrorLog /var/log/httpd/http_error.log
    TransferLog /var/log/httpd/http_transfer.log

    RedirectMatch permanent ^/(.*)$ https://sub-domain.mydomain.com/

</VirtualHost>