如何在 Apache 上将 HTTP 重定向到 HTTPS?

Tom*_*del 1 apache ssl ssl-certificate server ubuntu-16.04

环境:Ubuntu 和 Apache。

尝试设置从 http 到 https 的自动重定向。

我努力了:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile    <path to your crt file>
    SSLCertificateKeyFile   <path to your private key file>

    # ...
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
Run Code Online (Sandbox Code Playgroud)

从 mydomain.com --- 至 ---> (https://) mydomain.com

有任何想法吗?

Ros*_*ing 5

确保您可以通过 HTTP 和 HTTPS 访问您的网站。还要确保 mod_rewrite 已启用,然后您可以将这些行添加到 .htaccess 文件中。

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)