将 http:// 重定向到 https:// 的方法

Ways to redirect http:// to https://

我们有一个 Drupal 8 应用程序托管在 Ubuntu 服务器上,使用 Apache、PHP 5.6 和 MySQL 5.6。我想知道我们可以通过多少种方式将域从 http:// 重定向到 https:// 协议。

我试过以下方法

  1. 使用重定向从虚拟主机配置文件。
  2. 在项目的 .htaccess 文件中启用重写规则并提供重定向条件。

当我使用上述方法时,当请求数量增加时,网站会崩溃。如果我删除重定向条件,即使有更多请求到达服务器,网站也能正常工作。

所以,我想知道有没有其他方法可以在不增加服务器负担的情况下将域从 http:// 重定向到 https:// 协议。

  • 在您的 httpd.conf 中配置 mod_rewrite 以实现这样的重定向

例如:

LoadModule rewrite_module  modules/mod_rewrite.so
RewriteEngine on
<VirtualHost *:80>
  <IfModule mod_rewrite.c>
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/ [L,R]
  </IfModule>
</VirtualHost>

LoadModule rewrite_module  modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*) https://%{HTTP_HOST}/ [L,R]

LoadModule rewrite_module  modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/ [L,R]