使用.htaccess文件将HTTP重定向到HTTPS

在网站的根目录中创建或者编辑.htaccess”文件

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$https://example.com/ [L,R=301]
如何使用Apache将所有HTTP流量重定向到HTTPS

使用虚拟主机将HTTP重定向到HTTPS

在Ubuntu,Debian上,我们将在“/etc/apache2 /sites-available”中找到虚拟主机文件。

$sudo vi /etc/apache2/sites-available/example.conf

在Redhat,CentOS,Fedora中,我们可以下面目录找到虚拟主机配置:

/etc/httpd/conf.d

要重定向HTTP流量,我们需要添加以下行:

Redirect permanent/https://example.com/

将其添加到“<virtualhost *:80>”中的某个位置即可:

<VirtualHost *:80>
	Redirect permanent/https://example.com/
</VirtualHost>

然后重新加载Apache 配置:

$sudo systemctl reload apache2 # Debian, Ubuntu
OR
$sudo systemctl reload httpd # Red Hat, CentOS, Fedora
日期:2020-07-07 20:56:38 来源:oir作者:oir