如何在 Apache 上将 HTTP 重定向到 HTTPS?
问题描述:
环境:Ubuntu 和 Apache.
Environment: Ubuntu with Apache.
尝试设置从 http 到 https 的自动重定向.
Trying to setup automatic redirection from http to 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>
和
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
从 mydomain.com --- 到 --->(https://) mydomain.com
From mydomain.com --- To ---> (https://) mydomain.com
有什么想法吗?
答
确保您可以通过 HTTP 和 HTTPS 访问您的网站.还要确保启用了 mod_rewrite,然后您可以将这些行添加到您的 .htaccess 文件中.
Make sure you can access your website both through HTTP and HTTPS. Also make sure mod_rewrite is enabled, then you can add these lines to your .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]